码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-08-18 11:15:19      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

仓库类

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()的唤醒。

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

标签:

原文地址:http://www.cnblogs.com/YLsY/p/5783230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!