簡體   English   中英

在Java中使用線程和信號量為特定線程類型實現Cyclicbarrier

[英]implementing cyclicbarrier using threads and semaphores for specific thread types in java

public class cyclicBarrier {
    private static int n;
    private static int count;
    private static semaphore mutex;
    private static semaphore turnstile;
    private static semaphore turnstile2;

    public cyclicBarrier(int n){
        this.n = n;
        this.count = 0;
        this.mutex = new semaphore(1);
        this.turnstile = new semaphore(0);
        this.turnstile2 = new semaphore(0);
    }

    public synchronized void down() throws InterruptedException{
        this.phase1();
        this.phase2();
    }

    private synchronized void phase1() throws InterruptedException {
        this.mutex.down();
        this.count++;
        if (this.count == this.n){
            for (int i=0; i< this.n; i++){
                          this.turnstile.signal();
                    }
        }
        this.mutex.signal();
        this.turnstile.down();
    }

    private synchronized void phase2() throws InterruptedException {
        this.mutex.down();
        this.count--;
        if (this.count == 0){
            for (int i=0; i< this.n; i++){
                          this.turnstile2.signal();
                    }
        }
        this.mutex.signal();
        this.turnstile2.down();
    }
}

&&這里是類信號量,以防萬一

public class semaphore{
    private int counter;

    public semaphore(int number){
        if (number > 0) {
            this.counter = number;
        }
    }

    public synchronized void signal(){
        this.counter++;
        notifyAll();
    }

    public synchronized void down() throws InterruptedException{
        while (this.counter <= 0){
            wait();
        }
        this.counter--;
    }
}

這是我編寫的用於使用線程實現Cyclicbarriers的代碼。 我從本書中獲得了偽代碼以及有關死鎖的注釋,因此我認為“雖然可能存在錯誤”還是可以的。 第一階段用於“到達線程” ,第二階段用於“一起在關鍵區域中運行線程” 我的問題如下...如何更改代碼以考慮特定類型的線程? 例如,有氫線和氧線,並且我希望它們在勢壘處每有2個氫原子和1個氧原子時執行bond()。 先感謝您。

您需要自己實現Cyclicbarriers嗎? (作業)還是可以使用JDK的實現? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

也許您可以擴展await()方法,以包括您的Atom類型,並添加一個方法來告訴您原子在屏障處正在等待什么,如果您發現說出H20的條件,則可以在此調用回調方法。外殼鍵(Atoms []原子)

除了給您答案外,我還會給您一個提示。 讓我知道您是否需要完整的答案。

首先,根據另一個問題的 回答來修復代碼-如果我對它的缺陷是正確的,那就是:)

其次,假設我們有m個線程和一個barrier(n) ,其中m> = 2n,每個線程

while True:
    noncritical()
    barrier.enter() # e.g. phase1()
    critical()
    barrier.leave() # e.g. phase2()

如果按照我的建議實現enter()離開() ,關鍵部分中可能會有2n個線程。 為了解決這個問題,我提出了以下結構:

class N_At_A_Time_Barrier(n):
    fields:
        occupied = Semaphore(n)
        barrier = Barrier(n)
    method enter():
        occupied.down()
        barrier.down()
    method leave():
        occupied.up()

在《 信號量小書》的術語中,所占用的是復用。 提示:解決H2O問題的方法與此非常相似。 我的解決方案使用Barrier(1 + 2)和其他... :)

(腳注:實現N_At_A_Time_Barrier有更有效的方法,其中每組n個線程都有第一個到達的線程建立自己的共享狀態,但這要復雜得多,我認為這與H2O不相關問題整齊。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM