Thursday 11 July 2019

Collection Interface - ArrayList : How to make array list thread safe

ArrayList is NOT ThreadSafe

sometimes you need to restrict access one thread at a time by implementing Synchronization.

Collection list3 = Collections.synchronizedList(Arrays.asList(1, 2, 3, 4, 5));
synchronized (list3) {
Iterator j = list3.iterator();
while (j.hasNext())
System.out.println(j.next());
}