📄 barriersynch.java
字号:
/* ========================================================= 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -