barriersynch.java

来自「this code is a parallel programming code」· Java 代码 · 共 59 行

JAVA
59
字号
/* =========================================================   Class to implement barrier sychronisation.   iveArrived(i) decrements this count and notifies All     the parameter is an id -- not necessary, but useful for tracing   =========================================================*/public class BarrierSynch{  int WaitingOn = 0;  int ThreadsLeft = 0;  /* ========================================================     CONSTRUCTOR     Set the number of threads waiting to Wait Count argument     ========================================================  */  public BarrierSynch(int WaitCount){    WaitingOn = WaitCount;    ThreadsLeft = WaitCount;      System.out.println("New Barrier " +"="+ WaitingOn);  }  /* ================================================     Decrement the count of threads outstanding.     Conditionally decrement the count of threads to     wait for next time.     if the count is zero tell everyone to wake up     If the count is more than zero then just wait()     The count should never be less than zero.     ================================================  */  public synchronized void iveArrived(int ident){    if (WaitingOn > 0){      WaitingOn--;    };//System.out.println("WAITING DECR " + WaitingOn + ident);      if (WaitingOn == 0){  // wake up everyone waiting on this barrier           notifyAll();      }      else{        System.out.println("WAITING " + ident); //TRACE: waiting in barrier	try{ wait();}	catch (InterruptedException e) { };      }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?