【问题标题】:implementing cyclicbarrier using threads and semaphores for specific thread types in java在java中使用线程和信号量为特定线程类型实现循环屏障
【发布时间】:2013-03-21 11:02:03
【问题描述】:
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() 。先感谢您。

【问题讨论】:

    标签: java multithreading semaphore cyclicbarrier


    【解决方案1】:

    我不会给你答案,而是给你一个提示。如果您需要完整的答案,请告诉我。

    首先,根据my answeryour other question 修复您的代码——如果我对它的缺陷是正确的,那就是:)

    其次,假设我们有 m 个线程和一个 barrier(n),其中 m >= 2n,每个线程都在做

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

    如果你按照我的建议实现 enter()leave(),你可能在临界区有 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()
    

    The Little book of Semaphores 的术语中,occupied 是一个Multiplex。提示:H2O 问题的解决方案与此非常相似。我的解决方案使用 Barrier(1+2) 和...其他东西:)

    (脚注:有更有效的方法来实现 N_At_A_Time_Barrier,其中每组 n 个线程都有自己的共享状态,由第一个到达的线程设置,但这要复杂得多,而且我认为它与 H2O 问题的关系并不密切。)

    【讨论】:

    • 这只是晚了 6 年,但我猜晚了总比没有好。谢谢你:)
    【解决方案2】:

    您需要自己实现 Cyclicbarriers 吗? (在家工作) 或者你可以使用JDK的实现吗? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html

    也许您可以扩展 await() 方法,以包含您的 Atom 种类,并添加一个方法来告诉您哪些原子在屏障处等待,如果您发现有说 H20 的条件,您可以调用回调方法,在这种情况下是键(原子[]原子)

    【讨论】:

    • 不,我必须自己实现。 :)
    • 这实际上是一个很好的答案,但是我必须跟踪数组并且我必须在其上放置一个互斥锁。所以只有一个线程可以添加/删除它。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2011-09-13
    • 2017-06-02
    • 2016-01-26
    • 2013-12-07
    • 2013-11-30
    • 1970-01-01
    相关资源
    最近更新 更多