javaスレッドは生産者の消費者問題を模擬します。


package com.test;
import java.util.*;

/**
 *     http://www.importnew.com/16453.html
 *   wait() synchronized()         
 * 1、       wait  
 * 2、synchronized()       ,       synchronized
 * 3、nority()        ,        ?
 **/

public class ProducerConsumerInJava {

    public static void main(String args[]) { 
        System.out.println("How to use wait and notify method in Java"); 
        System.out.println("Solving Producer Consumper Problem"); 
        Queue buffer = new LinkedList();//       10                
        int maxSize = 10; 
        Thread producer = new Producer(buffer, maxSize, "PRODUCER"); 
        Thread consumer = new Consumer(buffer, maxSize, "CONSUMER"); 
        producer.start();//       
        consumer.start(); ///       
        } 
    } 

    class Producer extends Thread 
    {
    /*
        *                     ,     ,     wait()    ,            ,  notify()       
        * */
        private Queue queue; 
        private int maxSize; 
        public Producer(Queue queue, int maxSize, String name){ 
            super(name); this.queue = queue; this.maxSize = maxSize; 
        } 
        @Override public void run() 
        { 
            while (true) 
                { 
                    synchronized (queue) { 
                        while (queue.size() == maxSize) { 
                            try { 
                                System.out .println("Queue is full, " + "Producer thread waiting for " + "consumer to take something from queue"); 
                                queue.wait(); 
                            } catch (Exception ex) { 
                                ex.printStackTrace(); } 
                            } 
                            Random random = new Random(); 
                            int i = random.nextInt(); 
                            System.out.println("Producing value : " + i); 
                            queue.add(i); 
                            queue.notifyAll(); 
                        } 
                    } 
                } 
            } 
/*
 *             ,      ,       ,         notify()               
 * */
    class Consumer extends Thread { 
        private Queue queue; 
        private int maxSize; 
        public Consumer(Queue queue, int maxSize, String name){ 
            super(name); 
            this.queue = queue; 
            this.maxSize = maxSize; 
        } 
        @Override public void run() { 
            while (true) { 
                synchronized (queue) { 
                    while (queue.isEmpty()) { 
                        System.out.println("Queue is empty," + "Consumer thread is waiting" + " for producer thread to put something in queue"); 
                        try { 
                            queue.wait(); 
                        } catch (Exception ex) { 
                            ex.printStackTrace(); 
                        } 
                    } 
                    System.out.println("Consuming value : " + queue.remove()); 
                    queue.notifyAll(); 
                } 
            } 
        } 
}