Good day. Interested in a simple Java example of how to organize multi-threaded write / read from a collection. thank
|
1 answer
The easiest way is probably to take advantage of synchronized collections:
List<C> list = Collections.synchronizedList(new ArrayList<C>()); Record and reading are automatically synchronized, you can work freely from different streams. This, however, concerns only add / delete: indexing or iteration requires synchronization:
list.add(new C()); synchronized (list) { Iterator i = list.iterator(); while (i.hasNext()) // работаем с i.next() } [ Documentation ]
|