700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 线程通信 生产者消费者问题案例 模拟来电提醒和接听电话

线程通信 生产者消费者问题案例 模拟来电提醒和接听电话

时间:2022-08-09 12:52:31

相关推荐

线程通信 生产者消费者问题案例 模拟来电提醒和接听电话

package com.cfqp;public class Phone {private static boolean flag = false;public static void main(String[] args) {Phone huaWei = new Phone();huaWei.run();}//手机开机了public void run() {//来电提醒线程new Thread(new Runnable() {@Overridepublic void run() {try {while (true) {synchronized (Phone.this) {if (!flag) {System.out.println("来电温馨提示,小朋友,有人呼叫你!");flag = true;Phone.this.notify();Phone.this.wait();}}}} catch (Exception e) {e.printStackTrace();}}}).start();//接听电话提new Thread(new Runnable() {@Overridepublic void run() {try {while(true){synchronized (Phone.this){if(flag){System.out.println("接听电话,我用了5分钟");Thread.sleep(5000);flag=false;Phone.this.notify();Phone.this.wait();}else{Phone.this.notify();Phone.this.wait();}}}} catch (Exception e) {e.printStackTrace();}}}).start();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。