import java.util.*; class Factory1{ class Producer extends Thread{ public void run(){ int num=1; while(true){ try{sleep(1000);}catch(InterruptedException e){} System.out.println("Adding job #"+num); addJob(num++); } } } class Consumer extends Thread{ public void run(){ while(true){ try{sleep(1500);}catch(InterruptedException e){} System.out.println("Removing job #"+removeJob()); synchronized(Factory1.this){ System.out.println("Size of queue: " + list.size()); } } } } ArrayList list= new ArrayList(); public static void main(String [] a){ new Factory1(); } synchronized void addJob(int num){ list.add(new Integer(num)); notify(); } synchronized int removeJob(){ while(list.isEmpty())//Kol od ein li ma lehorid try{wait();}catch(Exception e){} int temp=((Integer)list.remove(0)).intValue(); notify(); return temp; } public Factory1(){ new Producer().start(); new Consumer().start(); } }