Reputation: 67
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
Upvotes: 1
Views: 110
Reputation: 667
First the design is seems to faulty , since developers and testers are distinct in their operation , it better to create a two class that implement Runnable and in place of CyclicBarrier use a CountDownLatch. Sample example based on your requirement :
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();
}
}
}
Once you have understood this snippet , you can try your own approaches to the problem
Hope that helps !!!
Upvotes: 1