private LinkedList<integer> list = new LinkedList<integer>();
    private final int LIMIT = 10;
    private Object lock = new Object();
 
    public void produce() throws InterruptedException {
       int value = 0;
        while (true) {
            synchronized (lock) {
                while(list.size() == LIMIT) {
                    lock.wait();
                }
                list.add(value++);
                lock.notify();
            }
        }
    }
    public void consume() throws InterruptedException {
        Random random = new Random();
        while (true) {
            synchronized (lock) {
                while(list.size() == 0) {
                    lock.wait();
                }
                System.out.print("List size is: " + list.size());
                int value = list.removeFirst();
                System.out.println("; value is: " + value);
                lock.notify();
            }
            Thread.sleep(random.nextInt(1000));
        }
    }
}

Source:http://www.caveofprogramming.com/java/java-multithreading-wait-and-notify-video-tutorial-producer-consumer-part-9/

Advertisements