wait与notify实现多线程的协调通信
来源:互联网 发布:知客文化传播 培训 编辑:程序博客网 时间:2024/06/01 22:08
public class Sample{public static void main(String args[]){SampleTest sampletest=new SampleTest();IncreaseThread it=new IncreaseThread(sampletest);DecreaseThread dt=new DecreaseThread(sampletest);it.start();dt.start();}}class SampleTest{private int number;public synchronized void increase(){while(number!=0){try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}number++;System.out.println(number);this.notify();}public synchronized void decrease(){while(number==0){try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}number--;System.out.println(number);this.notify();}}class IncreaseThread extends Thread{private SampleTest sampletest;public IncreaseThread(SampleTest sampletest){this.sampletest=sampletest;}@Overridepublic void run(){// TODO Auto-generated method stubfor(int i=0;i<20;i++){try {Thread.sleep((long)(Math.random()*1000));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}sampletest.increase();}}}class DecreaseThread extends Thread{private SampleTest sampletest;public DecreaseThread(SampleTest sampletest){this.sampletest=sampletest;}@Overridepublic void run(){// TODO Auto-generated method stubfor(int i=0;i<20;i++){try {Thread.sleep((long)(Math.random()*1000));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}sampletest.decrease();}}}
0 0
- wait与notify实现多线程的协调通信
- 多线程间的通信 wait()、notify()、notifyAll()
- wait()与notify()实现线程间的通信
- thread的wait\notify实现线程通信
- thread的wait\notify实现线程通信
- 线程通信,wait()与notify()的运用
- Java多线程通信-利用传统的线程通信wait(),notify()方法实现“生产者消费者模式”
- 多线程的wait()/notify()
- 多线程 : 使用 wait 和 notify 实现进程间同步通信
- java使用轮询和wait()/notify()实现多线程之间的通信
- Java【多线程知识总结(8)】线程通信,wait()与notify()的运用
- 【郭林专刊】多线程知识总结线程通信,wait()与notify()的运用
- Java【多线程知识总结(8)】线程通信,wait()与notify()的运用
- 【Java多线程与并发库】04 传统的线程同步通信技术-synchronized/wait/notify/notifyAll
- 多线程并发之线程间的通信,notify,wait
- 多线程编程入门(2):线程的通信(wait,notify)
- JAVA多线程通信之wait和notify的使用
- 多线程中的wait与notify
- myeclipse8.5集成jprofiler7.2.3插件
- Unity3d GUI适应分辨率
- C语言中如何使用宏 包括单双井号 可变参数
- 算法实验二:微软面试题——关于整数组最小差值的问题。
- [php扩展开发和嵌入式]第17章-php源代码的配置和链接
- wait与notify实现多线程的协调通信
- 老调重弹:JDBC系列 之 <驱动加载原理全面解析>
- 最长上升子序列
- mtk LCD 驱动过程详解
- NetMap中查看Esri shape文件
- UNIX中的标准IO库
- mac下给手机安装apk包
- 微乐的六维裂变
- poj-1151-Atlantis-线段树求面积并