How to make an ArrayList Synchronized


By default ArrayList is not synchronized and Vector is Synchronized.
But we can also make an ArrayList synchronized using the following code.
But if want to iterate that list then we have to iterate that list within the synchronized block.

		List<String> list = new ArrayList<String>();
		list.add("rahul");
		list.add("thakur");
		// Use the following collections method to make an ArrayList synchronized
		Collections.synchronizedList(list);	
                synchronized (list) {
			Iterator<String> iterator = list.iterator();
			while (iterator.hasNext()) {
				String string = (String) iterator.next();
			}
		}