【例程——生产者和消费者模型】使用wait方法和notify方法

【例程一】交替输出奇偶数

public class ThreadTest16 {
    public static void main(String[] args) {
        //共享对象(仓库)
        List list = new ArrayList();


        //创建两个线程对象
        //生产者线程
        Thread t1 = new Thread(new Producer(list));
        //消费者线程
        Thread t2 = new Thread(new Consumer(list));

        t1.setName("生产者生产");
        t2.setName("消费者消费");

        t1.start();
        t2.start();
    }
}

//生产者线程
class Producer implements Runnable {
    //仓库
    private List list;

    public Producer(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        //一直生产(用死循环模拟一直生产)
        while (true){

            synchronized (list){
                if (list.size() > 0){
                    try {
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            //程序能执行到此处说明仓库是空的,需要生产
            Object obj = new Object();
            list.add(obj);
            System.out.println(Thread.currentThread().getName() + "--->" +obj);
            //唤醒消费者进行消费
            list.notifyAll();
            }
        }
    }
}

//消费者线程
class Consumer implements Runnable{

    //仓库
    private List list;

    public Consumer(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        //一直消费
        while (true){
            synchronized (list){
                if (list.size() == 0){
                    //仓库空了,消费者进程等待
                    try {
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            //程序执行到此处说明仓库是满的,需要消费
            Object obj = list.remove(0);
            System.out.println(Thread.currentThread().getName() + "--->" + obj);
            //唤醒生产者生产
            list.notifyAll();
            }
        }
    }
}

【例程二】生产+消费

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

        //创建共享数字对象
        Num num = new Num();
        //创建两条线程
        //奇数线程
        Thread t1 = new Thread(new OddNum(num));
        //偶数线程
        Thread t2 = new Thread(new EvenNum(num));

        //修改线程名称
        t1.setName("t1");
        t2.setName("t2");

        //启动线程
        t1.start();
        t2.start();
    }
}

//共享数字对象
class Num{
    int i = 1;
}

//偶数线程
class EvenNum implements Runnable{
    //共享数字
    private Num num;

    public EvenNum(Num num) {
        this.num = num;
    }

    @Override
    public void run() {
        //死循环i++
        while(true){
            synchronized (num){
                if (num.i % 2 == 1){
                    //如果num是奇数,偶数线程进入等待状态
                    try {
                        num.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //程序进行到这说明是偶数,输出,并对数字进行++操作
                System.out.println(Thread.currentThread().getName() + "--->" + (num.i++));
                
                //一秒输出一次
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                //唤醒奇数线程(?)
                num.notify();
            }
        }
    }
}

//奇数线程
class OddNum implements Runnable{
    private Num num;

    public OddNum(Num num) {
        this.num = num;
    }

    @Override
    public void run() {
        while (true){
            synchronized (num){
                if (num.i % 2 == 0){
                    try {
                        num.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //执行到这说明是奇数,输出,并对数字进行++操作
                System.out.println(Thread.currentThread().getName() +"--->" + (num.i++));
                //一秒输出一次
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //唤醒偶数线程(?)
                num.notify();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值