对具有不同线程数的两个对象使用 CycleBarrier

Using CycleBarrier for two objects with different number of threads

我手头有以下任务:

假设有 4 名开发人员为 IT 解决方案实施功能,2 名测试人员验证他们的工作。 项目经理为这个项目制定了一个(有争议的)策略: 他不希望测试人员在所有开发人员完成他们的工作之前开始验证此应用程序。 此外,他不希望开发人员在所有测试人员完成工作之前就开始修复报告的缺陷(更改应用程序)。 要协调这些任务,请使用 CyclicBarrier class.

经过一些研究和教程后,我设法编译了以下代码,以便为每个 CycleBarrier 使用 2 个线程:

    public static void main(String[] args) {
    Runnable barrier1Action = new Runnable() {
        public void run() {
            System.out.println("Developers start working -> Testers start working");
        }
    };
    Runnable barrier2Action = new Runnable() {
        public void run() {
            System.out.println("Testers finish working -> Developers start fixing defects");
        }
    };

    CyclicBarrier barrier1 = new CyclicBarrier(2, barrier1Action);
    CyclicBarrier barrier2 = new CyclicBarrier(2, barrier2Action);


    CyclicBarrierRunnable barrierRunnable1 = new CyclicBarrierRunnable(barrier1, barrier2);

    CyclicBarrierRunnable barrierRunnable2 = new CyclicBarrierRunnable(barrier1, barrier2);


    new Thread(barrierRunnable1).start();
    new Thread(barrierRunnable2).start();

和 CyclicBarrierRunnable class:

public class CyclicBarrierRunnable implements Runnable {

    CyclicBarrier barrier1 = null;
    CyclicBarrier barrier2 = null;

    public CyclicBarrierRunnable(CyclicBarrier barrier1, CyclicBarrier barrier2) {

        this.barrier1 = barrier1;
        this.barrier2 = barrier2;
    }

    public void run() {
        try {

            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " waiting at barrier 1");
            this.barrier1.await();

            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " waiting at barrier 2");
            this.barrier2.await();

            System.out.println(Thread.currentThread().getName() + " done!");

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}

我的问题是,如果第一个 CyclicBarrier 有 4 个而不是 2 个,我如何向 运行 提出申请。如果我尝试 运行 它只有第一个 CyclicBarrier barrier1 = new CyclicBarrier(4, barrier1Action); CyclicBarrier barrier2 = new CyclicBarrier(2, barrier2Action);将启动 2 个线程

首先,设计似乎有问题,因为开发人员和测试人员的操作不同,最好创建两个 class 实现 Runnable 并使用 CountDownLatch 代替 CyclicBarrier。 根据您的要求示例:

import java.util.concurrent.CountDownLatch;

public class DevTest {

    static class Developer implements Runnable {

        CountDownLatch waitingForDeveloper=null;
        CountDownLatch waitingForTester   =null;

        public Developer(CountDownLatch waitingForDeveloper,CountDownLatch waitingForTester) {
            this.waitingForDeveloper=waitingForDeveloper;
            this.waitingForTester=waitingForTester;

        }
        @Override
        public void run(){

            System.out.println("Dev "+Thread.currentThread().getName()+" has completed the task");
            waitingForDeveloper.countDown();
            try{
                waitingForTester.await();
            }catch(InterruptedException ex){}
            System.out.println("Dev "+Thread.currentThread().getName()+" is Working on the bug raised by testers.");


        }
    }

    static class Tester implements Runnable {

        CountDownLatch waitingForDeveloper=null;
        CountDownLatch waitingForTester   =null;

        public Tester(CountDownLatch waitingForDeveloper,CountDownLatch waitingForTester) {
            this.waitingForDeveloper=waitingForDeveloper;
            this.waitingForTester=waitingForTester;

        }
        @Override
        public void run(){

            try{
                waitingForDeveloper.await();
            }catch(InterruptedException ex){}

            System.out.println("Tester "+Thread.currentThread().getName()+" has completed testing.");
            waitingForTester.countDown();

        }
    }
    public static void main(String[] args) {

        int noOFdevelopers=7;
        int noOFTesters=4;
        CountDownLatch waitingForDeveloper=new CountDownLatch(noOFdevelopers);
        CountDownLatch waitingForTester   =new CountDownLatch(noOFTesters);

        Developer developer=new Developer(waitingForDeveloper, waitingForTester);
        Tester    tester=new Tester(waitingForDeveloper, waitingForTester);

        for(int i=1;i<=noOFTesters;i++){
            new Thread(tester,""+i).start();
        }       
        for(int i=1;i<=noOFdevelopers;i++){
            new Thread(developer,""+i).start();
        }


    }
}

一旦你理解了这个片段,你就可以尝试你自己的方法来解决这个问题

希望对您有所帮助!!!