java线程通信

来源:互联网 发布:北京时空视点知乎 编辑:程序博客网 时间:2024/05/17 09:09

1.传统的线程通信,直接上代码:

<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 1main thread sequence of 2main thread sequence of 3main thread sequence of 4main thread sequence of 5main thread sequence of 6main thread sequence of 7main thread sequence of 8main thread sequence of 9main thread sequence of 10main thread sequence of 11main thread sequence of 12main thread sequence of 13main thread sequence of 14main thread sequence of 15main thread sequence of 16main thread sequence of 17main thread sequence of 18main thread sequence of 19main thread sequence of 20main thread sequence of 21main thread sequence of 22main thread sequence of 23main thread sequence of 24main thread sequence of 25main thread sequence of 26main thread sequence of 27main thread sequence of 28main thread sequence of 29main thread sequence of 30main thread sequence of 31main thread sequence of 32main thread sequence of 33main thread sequence of 34main thread sequence of 35main thread sequence of 36main thread sequence of 37main thread sequence of 38main thread sequence of 39main thread sequence of 40main thread sequence of 41main thread sequence of 42main thread sequence of 43main thread sequence of 44main thread sequence of 45main thread sequence of 46main thread sequence of 47main thread sequence of 48main thread sequence of 49main thread sequence of 50sub2 thread sequence of 1sub2 thread sequence of 2sub2 thread sequence of 3sub2 thread sequence of 4sub2 thread sequence of 5sub2 thread sequence of 6sub2 thread sequence of 7sub2 thread sequence of 8sub2 thread sequence of 9sub2 thread sequence of 10sub3 thread sequence of 1sub3 thread sequence of 2sub3 thread sequence of 3sub3 thread sequence of 4sub3 thread sequence of 5sub3 thread sequence of 6sub3 thread sequence of 7sub3 thread sequence of 8sub3 thread sequence of 9sub3 thread sequence of 10sub3 thread sequence of 11sub3 thread sequence of 12sub3 thread sequence of 13sub3 thread sequence of 14sub3 thread sequence of 15sub3 thread sequence of 16sub3 thread sequence of 17sub3 thread sequence of 18sub3 thread sequence of 19sub3 thread sequence of 20main thread sequence of 1main thread sequence of 2main thread sequence of 3main thread sequence of 4main thread sequence of 5main thread sequence of 6main thread sequence of 7main thread sequence of 8main thread sequence of 9main thread sequence of 10main thread sequence of 11main thread sequence of 12main thread sequence of 13main thread sequence of 14main thread sequence of 15main thread sequence of 16main thread sequence of 17main thread sequence of 18main thread sequence of 19main thread sequence of 20main thread sequence of 21main thread sequence of 22main thread sequence of 23main thread sequence of 24main thread sequence of 25main thread sequence of 26main thread sequence of 27main thread sequence of 28main thread sequence of 29main thread sequence of 30main thread sequence of 31main thread sequence of 32main thread sequence of 33main thread sequence of 34main thread sequence of 35main thread sequence of 36main thread sequence of 37main thread sequence of 38main thread sequence of 39main thread sequence of 40main thread sequence of 41main thread sequence of 42main thread sequence of 43main thread sequence of 44main thread sequence of 45main thread sequence of 46main thread sequence of 47main thread sequence of 48main thread sequence of 49main thread sequence of 50sub2 thread sequence of 1sub2 thread sequence of 2sub2 thread sequence of 3sub2 thread sequence of 4sub2 thread sequence of 5sub2 thread sequence of 6sub2 thread sequence of 7sub2 thread sequence of 8sub2 thread sequence of 9sub2 thread sequence of 10sub3 thread sequence of 1sub3 thread sequence of 2sub3 thread sequence of 3sub3 thread sequence of 4sub3 thread sequence of 5sub3 thread sequence of 6sub3 thread sequence of 7sub3 thread sequence of 8sub3 thread sequence of 9sub3 thread sequence of 10sub3 thread sequence of 11sub3 thread sequence of 12sub3 thread sequence of 13sub3 thread sequence of 14sub3 thread sequence of 15sub3 thread sequence of 16sub3 thread sequence of 17sub3 thread sequence of 18sub3 thread sequence of 19sub3 thread sequence of 20main thread sequence of 1main thread sequence of 2main thread sequence of 3main thread sequence of 4main thread sequence of 5main thread sequence of 6main thread sequence of 7main thread sequence of 8main thread sequence of 9main thread sequence of 10main thread sequence of 11main thread sequence of 12main thread sequence of 13main thread sequence of 14main thread sequence of 15main thread sequence of 16main thread sequence of 17main thread sequence of 18main thread sequence of 19main thread sequence of 20main thread sequence of 21main thread sequence of 22main thread sequence of 23main thread sequence of 24main thread sequence of 25main thread sequence of 26main thread sequence of 27main thread sequence of 28main thread sequence of 29main thread sequence of 30main thread sequence of 31main thread sequence of 32main thread sequence of 33main thread sequence of 34main thread sequence of 35main thread sequence of 36main thread sequence of 37main thread sequence of 38main thread sequence of 39main thread sequence of 40main thread sequence of 41main thread sequence of 42main thread sequence of 43main thread sequence of 44main thread sequence of 45main thread sequence of 46main thread sequence of 47main thread sequence of 48main thread sequence of 49main thread sequence of 50sub2 thread sequence of 1sub2 thread sequence of 2sub2 thread sequence of 3sub2 thread sequence of 4sub2 thread sequence of 5sub2 thread sequence of 6sub2 thread sequence of 7sub2 thread sequence of 8sub2 thread sequence of 9sub2 thread sequence of 10sub3 thread sequence of 1sub3 thread sequence of 2sub3 thread sequence of 3sub3 thread sequence of 4sub3 thread sequence of 5sub3 thread sequence of 6sub3 thread sequence of 7sub3 thread sequence of 8sub3 thread sequence of 9sub3 thread sequence of 10sub3 thread sequence of 11sub3 thread sequence of 12sub3 thread sequence of 13sub3 thread sequence of 14sub3 thread sequence of 15sub3 thread sequence of 16sub3 thread sequence of 17sub3 thread sequence of 18sub3 thread sequence of 19sub3 thread sequence of 20




1 0
原创粉丝点击