java 多个线程之间同步通信
程序员文章站
2022-05-28 18:24:51
...
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Lock 类似 synchronize (让多个线程 执行任务的时候 相互不干扰,不被打断 :互斥) * Condition (让多个线程之间 进行 交流 :通信) * * 例子 : 老大 --> 老二 -->老三 --> 老大 --> 老二 -->老三 */ public class ThreeConditionCommunication2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final Business bus=new ThreeConditionCommunication2().new Business(); new Thread( new Runnable(){ @Override public void run() { for (int j = 0; j < 5; j++) { bus.sub2(j); } } } ).start(); new Thread( new Runnable(){ @Override public void run() { for (int j = 0; j < 5; j++) { bus.sub3(j); } } } ).start(); for (int j = 0; j < 5; j++) { bus.sub1(j); } } //主要是这里不同 class Business{ Lock lock=new ReentrantLock(); Condition con1=lock.newCondition(); // 1 - 2 通信 Condition con2=lock.newCondition(); // 2 - 3 通信 int step=1; public void sub1(int i){ lock.lock(); try { while(step!=1) con1.await(); for (int j = 0; j < 100; j++) { System.out.println("main thread sequence of "+j+" loop of"+i); } step=2; con1.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } } public void sub2(int i){ lock.lock(); try { while(step!=2) con1.await(); for (int j = 0; j < 10; j++) { System.out.println("Sub2 thread sequence of "+j+" loop of"+i); } step=3; con2.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } } public void sub3(int i){ lock.lock(); try { while(step!=3) con2.await(); for (int j = 0; j < 20; j++) { System.out.println("Sub3 thread sequence of "+j+" loop of"+i); } step=1; con1.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } } } }
/** * Lock 类似 synchronize (让多个线程 执行任务的时候 相互不干扰,不被打断 :互斥) * Condition (让多个线程之间 进行 交流 :通信) * * 例子 :三个线程之间的互斥 和 通信 */ public class ThreeConditionCommunication { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub final Business bus=new ThreeConditionCommunication().new Business(); new Thread( new Runnable(){ @Override public void run() { for (int j = 0; j < 5; j++) { bus.sub2(j); } } } ).start(); new Thread( new Runnable(){ @Override public void run() { for (int j = 0; j < 5; j++) { bus.sub3(j); } } } ).start(); for (int j = 0; j < 5; j++) { bus.sub1(j); } } // 三个 Condition class Business{ Lock lock=new ReentrantLock(); Condition con1=lock.newCondition(); Condition con2=lock.newCondition(); Condition con3=lock.newCondition(); int step=1; public void sub1(int i){ lock.lock(); try { while(step!=1) con1.await(); for (int j = 0; j < 20; j++) { System.out.println("main thread sequence of "+j+" loop of"+i); } step=2; con2.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } } public void sub2(int i){ lock.lock(); try { while(step!=2) con2.await(); for (int j = 0; j < 10; j++) { System.out.println("Sub2 thread sequence of "+j+" loop of"+i); } step=3; con3.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } } public void sub3(int i){ lock.lock(); try { while(step!=3) con3.await(); for (int j = 0; j < 20; j++) { System.out.println("Sub3 thread sequence of "+j+" loop of"+i); } step=1; con1.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } } } }
上一篇: Java中如何取得midi音乐的最大和弦数 音乐JavaJ#
下一篇: PDO