package concurrence;

import java.util.Iterator;
import java.util.Vector;

public class ConcurrenceClass {
	
	private static Vector<Integer> data = new Vector<Integer>();
	
	public static void main(String[] args) throws Exception{
		new Producer().start();
		new Consumer().start();
	}

	static class Consumer extends Thread{
		
		public Consumer() {
			super("Consumer");
		}
		
		public void run(){
			for(;;){
				try {
					Thread.sleep(1);
				} catch (Exception e) {
					e.printStackTrace();
				}
				synchronized (data) {
					Iterator<Integer> it = data.iterator();
					while (it.hasNext()) {
						it.next();
						
					}
				}
				
			}
		}		
	}
	
	static class Producer extends Thread{
		
		public Producer() {
			super("Producer");
		}
		
		public void run(){
			for(;;){
				try {
					Thread.sleep(1);
				} catch (Exception e) {
					e.printStackTrace();
				}
				data.addElement(12);
				if (data.size() > 1000) {
					data.removeAllElements();
				}
			}
		}
		
	}
	
}
