public static void main(String... args) throws InterruptedException { ArrayList<Integer> list = new ArrayList<>(); Collections.addAll(list, new Integer[10000]); System.out.println("initial size: " + list.size());
final ExecutorService e = Executors.newFixedThreadPool(10); int size = list.size(); for (int i = 0; i < size; i++) { e.execute(() -> list.remove(0)); } e.shutdown(); e.awaitTermination(1000, TimeUnit.SECONDS); System.out.println(list.size());//should be zero } }
public static void main(String... args) throws InterruptedException { List<Integer> integers = new ArrayList<>(); List<Integer> list = Collections.synchronizedList(integers); Collections.addAll(list, new Integer[10000]); System.out.println("initial size: " + list.size());
final ExecutorService e = Executors.newFixedThreadPool(10); int size = list.size(); for (int i = 0; i < size; i++) { e.execute(() -> { list.remove(0); }); } e.shutdown(); e.awaitTermination(1000, TimeUnit.SECONDS); System.out.println(list.size());//should be zero } }