4-使用synchronized实现线程间通信-实现线程间通信方式(1)
来源:互联网 发布:mysql存储图片 编辑:程序博客网 时间:2024/06/02 02:57
一、主要内容
实现子线程运行10次,主线程运行100次,子线程、主线程交替进行,此过程循环进行50次。
二、代码实现
package cn.yjx.thread;public class TraditionalThreadCommunication {public static void main(String[] args) {final Business business = new Business();// 子线程new Thread(new Runnable() {@Overridepublic void run() {for(int i=1;i<=50;i++){business.sub(i);}}}).start();// 主线程for(int i=1;i<=50;i++){business.main(i);}}} class Business { private boolean bShouldSub = true; // 线程间进行通信的变量 // 子 public synchronized void sub(int i){ while(!bShouldSub){ try {this.wait();} catch (InterruptedException e) {e.printStackTrace();} }for(int j=1;j<=10;j++){System.out.println("sub thread sequence of " + j + ",loop of " + i);} bShouldSub = false; this.notify(); } // 主 public synchronized void main(int i){ while(bShouldSub){ try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} }for(int j=1;j<=100;j++){System.out.println("main thread sequence of " + j + ",loop of " + i);}bShouldSub = true;this.notify(); } }
0 0
- 4-使用synchronized实现线程间通信-实现线程间通信方式(1)
- 12-使用java5条件阻塞condition实现线程间通信-实现线程间通信方式(2)
- 线程间实现通信
- 使用WaitForMultipleObjects实现线程间通信
- 使用wait/notify实现线程间通信
- 18-java5阻塞队列实现线程间通信-实现线程间通信方式(3)
- 线程间通信方式
- 线程间通信方式
- 线程间通信方式
- IOS-62-实现进程间、线程间通信的方式
- Java线程间通信-回调的实现方式
- Java线程间通信-回调的实现方式
- Java线程间通信-回调的实现方式
- Java线程间通信-回调的实现方式
- Java线程间通信-回调的实现方式
- java实现线程间通信的四种方式
- android线程间通信的四种实现方式
- aidl实现线程间通信(实现步骤)
- UVA 10130 SuperSale (DP 01背包)
- time.h
- JS中日期和时间的获取
- 请教一下 服务器的环境是用PHPstudy搭建的用的80口?
- Singleton pattern
- 4-使用synchronized实现线程间通信-实现线程间通信方式(1)
- Decorator pattern
- 大学三年
- 蓝桥杯带分数
- poj 1250 Tanning Salon
- Sicily 2401. Judges' Time Calculation
- Pre,in,post expression
- Sicily 1533. CIVIC DILL MIX
- Mongo-cxx-driver Windows平台编译记录