I have the following task at hand:
Imagine there are 4 developers implementing features for an IT solution and 2 testers to validate their work. The project manager makes a (debatable) strategy for this project: He does not want the testers to start the validation of this application until all developers complete their work. Furthermore, he does not want the developers to start fixing the reported defects (changing the application) before all the testers complete their work. To coordinate these tasks, use the CyclicBarrier class.
After some research and tutorials I managed to compile the following code to work with 2 threads for each CycleBarrier:
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();
and the 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();
}
}
}
My question is how can i make the application to run if the first CyclicBarrier has 4 parties instead of 2. If i try to run it with CyclicBarrier barrier1 = new CyclicBarrier(4, barrier1Action);
CyclicBarrier barrier2 = new CyclicBarrier(2, barrier2Action);
only the first 2 threads will start