Online Java Compiler By
JavaTpoint.com
import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class CollectionsSynchronizedListExample3 { public static void main(String[] args) throws InterruptedException { List
integers = new ArrayList<>(); List
list = Collections.synchronizedList(integers); Collections.addAll(list, new Integer[10000]); System.out.println("Initial size: " + list.size()); final ExecutorService e = Executors.newFixedThreadPool(10); for (int i = 0; i < list.size(); i++) { e.execute(() -> list.remove(0)); } e.shutdown(); e.awaitTermination(1000, TimeUnit.SECONDS); System.out.println(list.size());//should be zero } }
Output