JAVA学习笔记(四十二)-生产者消费者模型

wait()、notify()

/*
 * wait()、notify()
 * 
 * 1.两个方法都只能在synchronized代码块中执行,因为要对持有锁的线程操作,只有同步中才有锁
 * 2.两个方法在操作同步中的线程时,必须要标识所操作线程持有的对象锁
 * 3.等待和唤醒必须是同一个对象锁
 */
public class Test05 {
    public static void main(String[] args) {
        MyThread3 mt=new MyThread3();
        Thread th=new Thread(mt,"线程一");
        th.start();

        try {
            Thread.sleep(2000);//主线程休眠2秒,让线程th在等待池中待一会
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //打断处于mt对象的等待池中的线程th
        //th.interrupt();

        synchronized (mt) {
            mt.notify();//唤醒对象mt的等待池中的一个线程
        }
    }
}

class MyThread3 implements Runnable{

    @Override
    public void run() {
        //同步代码块
        synchronized (this) {
            System.out.println("***进入同步代码块");
            try {
                wait();//线程进入wait池,处于阻塞状态
                System.out.println("***wait之后重新拿到了锁");
            } catch (InterruptedException e) {
                System.out.println("***"+Thread.currentThread().getName()+"被打断!");
            }
            for(int i=1;i<=20;i++){
                System.out.println(Thread.currentThread().getName()+"***"+i);
            }
            System.out.println("***同步代码块执行结束!");
        }
    }
}

生产者、消费者模型

/*
 * 线程间通信:多个线程操作同一个资源,但操作的动作不同
 * 生产者、消费者模型
 * wait()和notify()方法一般应用于生产者和消费者模型中,用来等待同步数据
 */
public class Test06 {
    public static void main(String[] args) {
        Person person = new Person();
        Input in = new Input(person);
        Output out = new Output(person);

        Thread th1 = new Thread(in,"生产者生产了: ");// 产生人
        Thread th2 = new Thread(out,"消费者消费了: ");// 取出人
        th1.start();
        th2.start();
    }
}

class Person {
    String name;
    String sex;
    boolean flag = true;// true表示需要产生人,false表示取出人
}

// 生产、入
class Input implements Runnable {
    private Person person;

    public Input(Person person) {
        this.person = person;
    }

    @Override
    public void run() {
        int x = 0;
        while (true) {
            synchronized (person) {
                if (person.flag) {
                    if (x == 0) {// 判断间隔产生不同的人
                        person.name = "tom";
                        person.sex = "male";
                    } else {
                        person.name = "张三";
                        person.sex = "女";
                    }
                    x = (x + 1) % 2; // 间隔产生不同的人
                    person.flag = false;// 放一次,取一次
                    person.notify();//唤醒取人的线程
                } else {
                    try {
                        person.wait();// 如果不需要产生人,则等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

// 消费、出
class Output implements Runnable {
    private Person person;

    public Output(Person person) {
        this.person = person;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (person) {
                if (!person.flag) {
                    System.out.println(Thread.currentThread().getName()+person.name + "***" + person.sex);
                    person.flag = true;
                    person.notify();//唤醒生产人的线程
                } else {
                    try {
                        person.wait();//如果不需要取出人,则等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值