通过消费者和生产者的多线程程序,了解Java的wait()和notify()用法

仓库类

public class Store {

    private int size = 0;//当前容量
    private final int MAX = 10;//最大容量
    
    //向仓库中增加货物
    public synchronized void add()
    {
        while(size >= MAX)//每次执行都要检查是否已满
        {
            try {
                wait();//如果满了,进入等待池等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        size++;//放入货物
        
        //输出相关信息
        System.out.println("*************Add***************");
        System.out.println(Thread.currentThread().toString());
        System.out.println("Size: " + Integer.toString(size));
        //唤醒等待池中的消费者线程
        notify();
    }
    
    //从仓库中取货物的方法
    public synchronized void remove()
    {
        while(size == 0)//每次执行前检查仓库中是否有货物
        {
            try {
                wait();//如果仓库为空,进入等待池
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        size--;//取走货物
        
        //输出相关信息
        System.out.println("*************Remove***************");
        System.out.println(Thread.currentThread().toString());
        System.out.println("Size: " + Integer.toString(size));
        //唤醒等待池中的生产者进程
        notify();
    }
    
    public int getSize()
    {
        return this.size;
    }
}

生产者类

public class Producer extends Thread{
    
    private Store s;
    
    public Producer(Store s) {
        this.s = s;
    }
    
    @Override
    public void run()
    {
        while(true)
        {
            s.add();//增加货物
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

消费者类

public class Customer extends Thread{

    Store s;
    
    public Customer(Store s)
    {
        this.s = s;
    }
    
    @Override
    public void run()
    {
        while(true)
        {
            s.remove();//取走货物
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Main

public class Main {

    public static void main(String[] args) {

        Store s = new Store();
        Producer p1 = new Producer(s);
        Producer p2 = new Producer(s);
        Producer p3 = new Producer(s);
        Customer c1 = new Customer(s);
        Customer c2 = new Customer(s);
        
        p1.setName("Producer1");
        p2.setName("Producer2");
        p3.setName("Producer3");
        c1.setName("Customer1");
        c2.setName("Customer2");
        
        p1.start();
        p2.start();
        p3.start();
        c1.start();
        c2.start();
    }

}

输出(部分)

*************Add***************
Thread[Producer1,5,main]
Size: 1
*************Remove***************
Thread[Customer2,5,main]
Size: 0
*************Add***************
Thread[Producer3,5,main]
Size: 1
*************Add***************
Thread[Producer2,5,main]
Size: 2
*************Remove***************
Thread[Customer1,5,main]
Size: 1
*************Add***************
Thread[Producer2,5,main]
Size: 2
*************Remove***************
Thread[Customer1,5,main]
Size: 1
*************Remove***************
Thread[Customer2,5,main]
Size: 0
*************Add***************
Thread[Producer1,5,main]
Size: 1
*************Add***************
Thread[Producer3,5,main]
Size: 2
*************Add***************
Thread[Producer2,5,main]
Size: 3
*************Remove***************
Thread[Customer2,5,main]
Size: 2
*************Add***************
Thread[Producer3,5,main]
Size: 3
*************Add***************
Thread[Producer1,5,main]
Size: 4
*************Remove***************
Thread[Customer1,5,main]
Size: 3
*************Add***************
Thread[Producer2,5,main]
Size: 4
*************Add***************
Thread[Producer1,5,main]
Size: 5
*************Remove***************
Thread[Customer1,5,main]
Size: 4
*************Add***************
Thread[Producer3,5,main]
Size: 5
*************Remove***************
Thread[Customer2,5,main]
Size: 4
*************Add***************
Thread[Producer2,5,main]
Size: 5
*************Remove***************
Thread[Customer1,5,main]
Size: 4
*************Add***************
Thread[Producer3,5,main]
Size: 5
*************Remove***************
Thread[Customer2,5,main]
Size: 4
*************Add***************
Thread[Producer1,5,main]
Size: 5

wait()可以让持有当前对象进入等待状态,等待notify()的唤醒。

posted on 2016-08-18 11:06  孜然狼  阅读(...)  评论(...编辑  收藏

导航

页面底部区域 foot.htm