java线程通信
2016-05-14 16:01
405 查看
1.传统的线程通信,直接上代码:
2.java JDK5之后的线程通信,直接上代码:
运行结果如下:
<pre name="code" class="java">package com.norelax.www; //传统的线程通信,主线程执行100次,子线程执行50次,主线程和子线程交替执行 public class TraditionalThreadCommunication { public static void main(String[] args) { final Business business=new Business(); new Thread(new Runnable() { public void run() { business.sub(); } }).start(); business.main(); } } class Business{ //标志位用来标示是否要执行子线程 boolean bShouldSub=true; //子线程执行方法 public synchronized void sub() { while (!bShouldSub) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 1; i <= 50; i++) { System.out.println("sub thread sequence of " +i); } bShouldSub=false; //唤醒主线程 this.notify(); } //主线程执行方法 public synchronized void main(){ while (bShouldSub) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for (int i = 1; i <= 100; i++) { System.out.println("main thread sequence of " +i); } bShouldSub=true; //唤醒子线程 this.notify(); } }
2.java JDK5之后的线程通信,直接上代码:
<pre name="code" class="java">package com.norelax.www; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 三种线程之间的通信问题 * * @author wusong 实例:主线程循环50次后休眠,叫醒子线程sub2执行10后休眠,叫醒子线程sub3执行20次后休眠,叫醒主线程 */ public class ThreeThreadCommunication { public static void main(String[] args) { final Business business = new Business(); new Thread(new Runnable() { public void run() { for (int i = 0; i < 10; i++) { business.sub2(); } } }).start(); new Thread(){ public void run() { for (int i = 0; i < 10; i++) { business.sub3(); } }}.start(); for (int i = 0; i < 10; i++) { business.main(); } } static class Business { Lock lock=new ReentrantLock(); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Condition condition3 = lock.newCondition(); //线程执行的标志位 int bShouldSub=1; //主线程方法 public void main(){ //加锁 lock.lock(); try { while (bShouldSub!=1) { condition1.await(); } for (int i = 1; i <=50; i++) { System.out.println("main thread sequence of "+i); } bShouldSub=2; //唤醒sub2线程 condition2.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ //最终锁要示释放 lock.unlock(); } } //子线程2的方法 public void sub2(){ //加锁 lock.lock(); try { while (bShouldSub!=2) { condition2.await(); } for (int i = 1; i <=10; i++) { System.out.println("sub2 thread sequence of "+i); } bShouldSub=3; //唤醒sub3线程 condition3.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ //最终锁要示释放 lock.unlock(); } } //子线程3的方法 public void sub3(){ //加锁 lock.lock(); try { while (bShouldSub!=3) { condition3.await(); } for (int i = 1; i <=20; i++) { System.out.println("sub3 thread sequence of "+i); } bShouldSub=1; //唤醒sub3线程 condition1.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ //最终锁要示释放 lock.unlock(); } } } }
运行结果如下:
main thread sequence of 1 main thread sequence of 2 main thread sequence of 3 main thread sequence of 4 main thread sequence of 5 main thread sequence of 6 main thread sequence of 7 main thread sequence of 8 main thread sequence of 9 main thread sequence of 10 main thread sequence of 11 main thread sequence of 12 main thread sequence of 13 main thread sequence of 14 main thread sequence of 15 main thread sequence of 16 main thread sequence of 17 main thread sequence of 18 main thread sequence of 19 main thread sequence of 20 main thread sequence of 21 main thread sequence of 22 main thread sequence of 23 main thread sequence of 24 main thread sequence of 25 main thread sequence of 26 main thread sequence of 27 main thread sequence of 28 main thread sequence of 29 main thread sequence of 30 main thread sequence of 31 main thread sequence of 32 main thread sequence of 33 main thread sequence of 34 main thread sequence of 35 main thread sequence of 36 main thread sequence of 37 main thread sequence of 38 main thread sequence of 39 main thread sequence of 40 main thread sequence of 41 main thread sequence of 42 main thread sequence of 43 main thread sequence of 44 main thread sequence of 45 main thread sequence of 46 main thread sequence of 47 main thread sequence of 48 main thread sequence of 49 main thread sequence of 50 sub2 thread sequence of 1 sub2 thread sequence of 2 sub2 thread sequence of 3 sub2 thread sequence of 4 sub2 thread sequence of 5 sub2 thread sequence of 6 sub2 thread sequence of 7 sub2 thread sequence of 8 sub2 thread sequence of 9 sub2 thread sequence of 10 sub3 thread sequence of 1 sub3 thread sequence of 2 sub3 thread sequence of 3 sub3 thread sequence of 4 sub3 thread sequence of 5 sub3 thread sequence of 6 sub3 thread sequence of 7 sub3 thread sequence of 8 sub3 thread sequence of 9 sub3 thread sequence of 10 sub3 thread sequence of 11 sub3 thread sequence of 12 sub3 thread sequence of 13 sub3 thread sequence of 14 sub3 thread sequence of 15 sub3 thread sequence of 16 sub3 thread sequence of 17 sub3 thread sequence of 18 sub3 thread sequence of 19 sub3 thread sequence of 20 main thread sequence of 1 main thread sequence of 2 main thread sequence of 3 main thread sequence of 4 main thread sequence of 5 main thread sequence of 6 main thread sequence of 7 main thread sequence of 8 main thread sequence of 9 main thread sequence of 10 main thread sequence of 11 main thread sequence of 12 main thread sequence of 13 main thread sequence of 14 main thread sequence of 15 main thread sequence of 16 main thread sequence of 17 main thread sequence of 18 main thread sequence of 19 main thread sequence of 20 main thread sequence of 21 main thread sequence of 22 main thread sequence of 23 main thread sequence of 24 main thread sequence of 25 main thread sequence of 26 main thread sequence of 27 main thread sequence of 28 main thread sequence of 29 main thread sequence of 30 main thread sequence of 31 main thread sequence of 32 main thread sequence of 33 main thread sequence of 34 main thread sequence of 35 main thread sequence of 36 main thread sequence of 37 main thread sequence of 38 main thread sequence of 39 main thread sequence of 40 main thread sequence of 41 main thread sequence of 42 main thread sequence of 43 main thread sequence of 44 main thread sequence of 45 main thread sequence of 46 main thread sequence of 47 main thread sequence of 48 main thread sequence of 49 main thread sequence of 50 sub2 thread sequence of 1 sub2 thread sequence of 2 sub2 thread sequence of 3 sub2 thread sequence of 4 sub2 thread sequence of 5 sub2 thread sequence of 6 sub2 thread sequence of 7 sub2 thread sequence of 8 sub2 thread sequence of 9 sub2 thread sequence of 10 sub3 thread sequence of 1 sub3 thread sequence of 2 sub3 thread sequence of 3 sub3 thread sequence of 4 sub3 thread sequence of 5 sub3 thread sequence of 6 sub3 thread sequence of 7 sub3 thread sequence of 8 sub3 thread sequence of 9 sub3 thread sequence of 10 sub3 thread sequence of 11 sub3 thread sequence of 12 sub3 thread sequence of 13 sub3 thread sequence of 14 sub3 thread sequence of 15 sub3 thread sequence of 16 sub3 thread sequence of 17 sub3 thread sequence of 18 sub3 thread sequence of 19 sub3 thread sequence of 20 main thread sequence of 1 main thread sequence of 2 main thread sequence of 3 main thread sequence of 4 main thread sequence of 5 main thread sequence of 6 main thread sequence of 7 main thread sequence of 8 main thread sequence of 9 main thread sequence of 10 main thread sequence of 11 main thread sequence of 12 main thread sequence of 13 main thread sequence of 14 main thread sequence of 15 main thread sequence of 16 main thread sequence of 17 main thread sequence of 18 main thread sequence of 19 main thread sequence of 20 main thread sequence of 21 main thread sequence of 22 main thread sequence of 23 main thread sequence of 24 main thread sequence of 25 main thread sequence of 26 main thread sequence of 27 main thread sequence of 28 main thread sequence of 29 main thread sequence of 30 main thread sequence of 31 main thread sequence of 32 main thread sequence of 33 main thread sequence of 34 main thread sequence of 35 main thread sequence of 36 main thread sequence of 37 main thread sequence of 38 main thread sequence of 39 main thread sequence of 40 main thread sequence of 41 main thread sequence of 42 main thread sequence of 43 main thread sequence of 44 main thread sequence of 45 main thread sequence of 46 main thread sequence of 47 main thread sequence of 48 main thread sequence of 49 main thread sequence of 50 sub2 thread sequence of 1 sub2 thread sequence of 2 sub2 thread sequence of 3 sub2 thread sequence of 4 sub2 thread sequence of 5 sub2 thread sequence of 6 sub2 thread sequence of 7 sub2 thread sequence of 8 sub2 thread sequence of 9 sub2 thread sequence of 10 sub3 thread sequence of 1 sub3 thread sequence of 2 sub3 thread sequence of 3 sub3 thread sequence of 4 sub3 thread sequence of 5 sub3 thread sequence of 6 sub3 thread sequence of 7 sub3 thread sequence of 8 sub3 thread sequence of 9 sub3 thread sequence of 10 sub3 thread sequence of 11 sub3 thread sequence of 12 sub3 thread sequence of 13 sub3 thread sequence of 14 sub3 thread sequence of 15 sub3 thread sequence of 16 sub3 thread sequence of 17 sub3 thread sequence of 18 sub3 thread sequence of 19 sub3 thread sequence of 20
相关文章推荐
- java异常处理
- Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException ContextLoaderL
- Spring中属性文件properties的读取与使用
- 【SSH】Struts2
- Java代码使用正则验证和常用工具方法
- JavaWeb学习总结(十二)——Session
- JAVA多线程之线程间的通信方式
- java集合List学习(结合源代码)
- java知识结构
- Java并发编程--原子性和可见性
- java操作Excel常用方法总结(POI)
- java集合相关知识
- java字符编码
- java jdbc数据库操作
- java25包和访问权限(一)
- 线程同步
- [原创]java WEB学习笔记03:使用eclipes开发javaWEB项目
- ant 打包配置 打jar 包配置
- Java对象判断“==”相等
- Java程序开发中的简单内存分析