Handle concurrent read/write: use synchronized to lock the data
import java.util.Iterator; import java.util.Vector; public class Main { Vector data = new Vector(); public Main() { new Producer().start(); new Consumer().start(); } public static void main(String[] args) throws Exception { new Main(); } class Consumer extends Thread { Consumer() { super("Consumer"); } public void run() { while (true) { synchronized (data) { Iterator it = data.iterator(); while (it.hasNext()) it.next(); } } } } class Producer extends Thread { Producer() { super("Producer"); } public void run() { while (true) { data.addElement(new Object()); if (data.size() > 1000) data.removeAllElements(); } } } }