厦门网站制作推广,wordpress默认ssl,wordpress博客访问,甘肃省城乡建设网站代码实现上述框图#xff1a; 1 //等待唤醒机制2 3 /*4 wait(),notify(),notifyAll()必须用在同步中#xff0c;因为同步中才有锁。5 指明让持有那个锁的线程去等待或被唤醒#xff0c;例如object.wait(),表明让持有object这把锁的线程等待。6 7 wait():让线程进入等待状态 1 //等待唤醒机制2 3 /*4 wait(),notify(),notifyAll()必须用在同步中因为同步中才有锁。5 指明让持有那个锁的线程去等待或被唤醒例如object.wait(),表明让持有object这把锁的线程等待。6 7 wait():让线程进入等待状态就是把线程放入了线程池。8 notify():唤醒线程池中的任意一个线程(持有特定锁的任意一个线程)。9 notifyAll():唤醒所有线程。10 11 wait(),notify(),notifyAll()为什么定义在Object中12 锁可以是任意的对象任意对象都可以调用的方法需要定义在Object中。13 */14 15 16 //描述数据17 class Resource 18 {19 public String name;20 public String gender;21 public boolean flag; //添加标记默认为false标志位的用途例如Input存完一组数据可能继续持有CPU存完后将flag置反则Input无法继续存入数据。22 public Resource(){}23 }24 25 //描述输入任务26 class Input implements Runnable27 {28 private Resource res;29 public Input(Resource res)30 {31 this.res res;32 }33 public void run()34 {35 int i 1;36 while(true)37 {38 synchronized(res) //加同步锁①39 {40 if(res.flag)41 { 42 //等待的线程会放弃锁跟sleep不同sleep的线程仍然拥有锁。43 try{res.wait();}catch(InterruptedException e){e.printStackTrace();} //判断flag标志先判断该不该存如果为true放弃CPU。44 }45 if(i1)46 {47 res.name 猪小明;48 res.gender 男;49 }50 else51 {52 res.name 腿腿;53 res.gender 女;54 }55 i(i)%2; //0、1切换56 res.flag true;57 res.notify(); //唤醒对方允许空唤醒。58 //try{res.wait();}catch(InterruptedException e){e.printStackTrace();}59 }60 }61 }62 }63 64 //描述输出任务65 class Output implements Runnable66 {67 private Resource res;68 public Output(Resource res)69 {70 this.res res;71 }72 public void run()73 {74 while(true)75 { 76 synchronized(res) //加同步锁②①处和此处为同一把锁77 {78 if(!res.flag)79 {80 try{res.wait();}catch(InterruptedException e){e.printStackTrace();} //判断flag标志先判断该不该存如果为false放弃CPU。81 }82 System.out.println(res.name ..... res.gender);83 res.flag false;84 res.notify(); //唤醒对方85 //try{res.wait();}catch(InterruptedException e){e.printStackTrace();}86 }87 }88 }89 }90 91 class TestDengdai92 {93 public static void main(String[] args)94 {95 //创建资源96 Resource res new Resource();97 //创建输入任务98 Input input new Input(res);99 //创建输出任务
100 Output output new Output(res);
101 //创建输入线程
102 Thread t1 new Thread(input);
103 //创建输出线程
104 Thread t2 new Thread(output);
105 //启动线程
106 t1.start();
107 t2.start();
108
109 }
110 } 上述代码实现存入一个输出一个的运行效果: 转载于:https://www.cnblogs.com/tzc1024/p/6040700.html