线程之“生产者消费者”

线程之“生产者消费者”

sleep():sleep来自Thread类,调用时线程睡眠但不释放当前对象锁(括号内加入睡眠时间,时间到后自然苏醒);

wait():wait来自Object类,调用线程睡眠释放当前对象锁定,进入等待线程池,出让系统资源,其他线程可以占用CPU资源,等待notify/notifyAll去对它进行唤醒后重新进入就绪态(括号内亦可加时间,与sleep方法类似);

synchronized:线程同步锁,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。

notify():随机唤醒等待池线程,线程优先级越高,分配资源越多,唤醒概率越大;
notifyAll():唤醒所有wait()的线程。

其中sleep需要捕获异常,wait,notify/notifyAll不需要。

本次实验
生产者:做馒头,每做一个休息0.5s,当馒头大于10个时停止生产。
消费者:吃馒头,每吃一个休息1s,当馒头小于等于2个时唤醒生产者继续生产。

package Thread;
//生产者
public class Producer extends Thread {

    private Container container;    

    public Producer(Container container){

        super();
        setName("生产者");
        this.setContainer(container);

    }


    public Container getContainer() {
        return container;
    }

    public void setContainer(Container container) {
        this.container = container;
    }

    public void run(){

        while(true){
            container.add();//生产馒头
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
package Thread;
//消费者
public class Consumer extends Thread {

private Container container;    

    public Consumer(Container container){

        super();
        setName("消费者");
        this.setContainer(container);

    }

    public Container getContainer() {
        return container;
    }

    public void setContainer(Container container) {
        this.container = container;
    }

    public void run(){

        while(true){
            container.sub();//吃馒头
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
package Thread;
//容器,把 **数量的判断和增减** 抽取出来使用sychronized进行同步
public class Container extends Thread {

    public int size = 0;
    /*public int count = 0;*/
    public synchronized void add(){
        String name = Thread.currentThread().getName();
        //数量判断
        if(size>=10){
            System.out.println(name + "说:装不下啦!!我先休息一下!");
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else {
            //数量增减
            System.out.println(name +"正在做馒头!"+ ++size);
            if(size>5){
                System.out.println("来吃馒头啦!!");
            }
            this.notify();
        }
    }

    public synchronized void sub(){

        String name = Thread.currentThread().getName();

        //数量判断
        if(size<=0){
            System.out.println(name +"说:没馒头啦");

            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else {
        //数量增减
            System.out.println(name + "吃馒头!!"+  --size);
            /*System.out.println(name + "我已经吃了"+  ++count +"个啦");*/
            /*if(count>=10){
                System.out.println(name +"吃不下了,休息一下!");
                try {
                    Thread.sleep(5000);
                    count=0;
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                this.notify();
            }*/
            //数量判断
            if(size<=2){
                System.out.println(name +"大吼:快来做馒头!!");
                this.notify();
            }
        }
    }
}

package Thread;

public class Test {
    public static void main(String[] args) {

        Container container = new Container();
        Producer producer = new Producer(container);
        Consumer consumer = new Consumer(container);

        producer.start();
        consumer.start();
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值