资源类
package Test22_Demo.Demo08;/*@create 2020--12--15--15:18*//*** 资源类* 定义共享的数据,其中成员:name,sex*/public class Person {//不使用private是为了便于后面的操作String name;String sex;public Person() {super();}}
消费者目标对象
package Test22_Demo.Demo08;/*@create 2020--12--15--15:29*//*** 消费者目标对象* 反复的获取Person对象并且打印*/public class GetOut implements Runnable{//实例化共享资源private Person person;//构造函数public GetOut(Person person) {this.person = person;}@Overridepublic void run() {//使用循环获取while (true) {//线程睡眠100毫秒try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}//获取实例String name = person.name;String sex = person.sex;System.out.println(person);System.out.println("消费了:"+ name+" "+sex);}}}
生产者目标对象
package Test22_Demo.Demo08;/*@create 2020--12--15--15:20*//*** 生产者* <p>* 生产者的执行目标:* 反复为Person对象赋值属性:Jack 男 与 Rose 女*/public class putIn implements Runnable {//实例化对象private Person person;//创建构造方法用于接收外部传入的共享数据Personpublic putIn(Person person) {this.person = person;}@Overridepublic void run() {//使用循环达到反复赋值的目的//如果是奇数就生成Jack 男 如果是偶数就生成rose 女int i = 0;while (true) {synchronized (person) {try {Thread.sleep(100);//每隔100毫秒} catch (InterruptedException e) {e.printStackTrace();}//判断生产的内容if (i % 2 == 1) {//男person.name = "Jack";person.sex = "男";} else {person.name = "Rose";person.sex = "女";}System.out.println(person);System.out.println("生产了"+person.name+"-"+person.sex);}i++;}}}
演示等待唤醒机制
package Test22_Demo.Demo08;/*@create 2020--12--15--15:15*//***演示等待和唤醒机制** 开启生产者和消费者的主要线程逻辑* 生产者和消费者的线程执行目标可以通过构造函数传入共同操作的同一个资源对象** 生产者和消费者线程安全的问题:* 将生产过程和消费过程都使用synchronized包裹代码块,并且使用共享对象作为锁**/public class WaiteNotifyDemo {public static void main(String[] args) {//实例化对象Person person = new Person();//创建生产者的执行目标putIn putIn = new putIn(person);//创建消费者的执行目标GetOut getOut = new GetOut(person);//开启生产者线程Thread putInThread = new Thread(putIn);putInThread.start();//开启消费者线程Thread getOutThread = new Thread(getOut);getOutThread.start();}}
- 解决方案:
- 等待唤醒的操作
- 在person类中声明一个flag - 状态
在生产者的执行目标中,先判断是否有数据
有:生产者进入等待(wait)<br />没有:正常生产<br />标记:true<br />唤醒消费者
在消费者线程执行目标中先判断是否有数据:
有:正常消费
没有:等待生产(wait)
标记:flase
唤醒生产者
补充
sleep方法和wait方法是否都释放锁:
- sleep方法由于线程会自动苏醒,所以不释放锁
- wait方法释放锁,由其他的线程唤醒等待的这个线程,被唤醒后,会继续执行wait后面的代码
**
- object中两个方法:
- 1.wait() - 进入等待
- 2.notify() - 唤醒后进入可运行状态
**
唤醒和等待:
资源类:
package Test22_Demo.Demo09;/*@create 2020--12--15--15:18*//*** 资源类* 定义共享的数据,其中成员:name,sex*/public class Person {//不使用private是为了便于后面的操作String name;String sex;//状态标记/*** flag:* true:有数据,生产者等待消费* false:没有数据,消费者等待*/boolean flag;public Person() {super();}}
消费者目标对象:
package Test22_Demo.Demo09;/*@create 2020--12--15--15:29*//*** 消费者目标对象* 反复的获取Person对象并且打印*/public class GetOut implements Runnable{//实例化共享资源private Person person;//构造函数public GetOut(Person person) {this.person = person;}@Overridepublic void run() {//使用循环获取while (true) {//利用共享对象作为锁synchronized (person) {//获取判断状态while (!person.flag) {//没有数据就等待try {person.wait();} catch (InterruptedException e) {e.printStackTrace();}}//有数据或者是刚刚被唤醒try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}//进行获取操作String name = person.name;String sex = person.sex;System.out.println(person);System.out.println("消费了" + name + "-" + sex);//切换状态person.flag = false;//唤醒生产者,如果有就唤醒,如果没有就无需唤醒person.notify();}}}}
生产者
package Test22_Demo.Demo09;/*@create 2020--12--15--15:20*//*** 生产者* <p>* 生产者的执行目标:* 反复为Person对象赋值属性:Jack 男 与 Rose 女*/public class putIn implements Runnable {//实例化对象private Person person;//创建构造方法用于接收外部传入的共享数据Personpublic putIn(Person person) {this.person = person;}@Overridepublic void run() {//使用循环达到反复赋值的目的//如果是奇数就生成Jack 男 如果是偶数就生成rose 女int i = 0;while (true) {synchronized (person) {//判断状态flagwhile (person.flag) {//有数据,就等待try {person.wait();//线程阻塞} catch (InterruptedException e) {e.printStackTrace();}}//如果没有数据或者刚刚被唤醒//判断生产的内容if (i % 2 == 1) {//男person.name = "Jack";try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}person.sex = "男";} else {person.name = "Rose";try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}person.sex = "女";}System.out.println(person);System.out.println("生产了"+person.name+"-"+person.sex);//切换状态为trueperson.flag = true;//如果消费者等待,就唤醒消费者,如果没有消费者等待就无需唤醒,相当于没有执行person.notify();}i++;}}}
等待和消费机制:
package Test22_Demo.Demo09;/*@create 2020--12--15--15:15*//***演示等待和唤醒机制** 开启生产者和消费者的主要线程逻辑* 生产者和消费者的线程执行目标可以通过构造函数传入共同操作的同一个资源对象** 生产者和消费者线程安全的问题:* 将生产过程和消费过程都使用synchronized包裹代码块,并且使用共享对象作为锁*** 解决方案:* 等待唤醒的操作** object中两个方法:* 1.wait() - 进入等待* 2.notify() - 唤醒后进入可运行状态*/public class WaiteNotifyDemo {public static void main(String[] args) {//实例化对象Person person = new Person();//创建生产者的执行目标putIn putIn = new putIn(person);//创建消费者的执行目标GetOut getOut = new GetOut(person);//开启生产者线程Thread putInThread = new Thread(putIn);putInThread.start();//开启消费者线程Thread getOutThread = new Thread(getOut);getOutThread.start();}}
**
