Gli arrayList in java possono gestire la concorrenza? nel senso, se io in 2 thread diversi, in uno inserisco un elemento e nell'altro ne tolgo uno, posso avere problemi (magari size sbagliata o altro)?
Gli arrayList in java possono gestire la concorrenza? nel senso, se io in 2 thread diversi, in uno inserisco un elemento e nell'altro ne tolgo uno, posso avere problemi (magari size sbagliata o altro)?
http://docs.oracle.com/javase/7/docs...ArrayList.html
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:
www.sitemeer.com » Quando un sito pare irraggiungibile
Se ti piace ci puoi trovare anche su Facebook
Dalla documentazione ufficiale:
ArrayList non è thread-safe ma ci sono vari modi per renderla tale..cerca su google e troverai molto materiale oppure lavora un po' con la fantasiaNote that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally...bla bla bla
ArrayList API
EDIT: mi hanno anticipato![]()
E c'è già qualche costrutto thread-safe?
Non vorrei dire cavolate ma penso che i Vector lo siano..
Oppure puoi ricorrere "all'artifizio"
codice:Collections.synchronizedList(List list)![]()
Esatto, è la differenza principale fra Vector ed ArrayList.Originariamente inviato da zipangulu
Non vorrei dire cavolate ma penso che i Vector lo siano..
Oppure puoi ricorrere "all'artifizio"
codice:Collections.synchronizedList(List list)![]()
effeffe
Quello che ti serve è la LinkedBlockingQueue
...