What is the result of the code?

import java.util.*;

public class TestIterator{
	public static void main(String[] args){
		final List synchList = Collections.synchronizedList(new ArrayList());
		fillList(synchList, 10);
		Thread anotherThread = new Thread(){
			public void run(){
				try{
					Thread.sleep(300);
				}catch (InterruptedException e){
					System.out.println("anotherThread interrupted");
					return;
				}
					synchList.remove(1);
					synchList.remove(7);
			}
		};
		anotherThread.start();
		Iterator iterator = synchList.iterator();
		while(iterator.hasNext()){
			System.out.print(iterator.next());
			try{
				Thread.sleep(100);
			} catch (InterruptedException e) {
				System.out.println("Main thread interrupted");
				return;
			}
		}
	}
	static void fillList(List list, int n){
		for(int i = 1; i <= n; i++)
			list.add(i);
	}
}
Explanation
Although the Collections class method returns the collection synchronized copy, the iterator stays not synchronized. As the result ConcurrentMofificationException will be thrown.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты