Reputation: 1086
I am learning how to use countdownLatch in java, and i created a simple example as shown below inthe code.
what i learnt about that mechanism is, it is just a way to force only ONE thread to wait for others till they finish their work, then that thread which was waiting , will starts it job when the other(s) thread(s) finish.
my question is, what if i have 4 threads 't1, t2, t3 and t4', and they should start in order as stated and each thread should start when the leading/previous one finishs. in other words, t2 should wait for t1 and starts when t1 finishes, and t3 should wait for t2 and starts when t2 finishes, t4 should wait for t3 and starts when t3 finihses.
1-how to do so using CountDownLatch and cyclic barrier?
2- is the countDown parameter passed to the constructor of the CountDownLatch class should represents the number of threads waiting?
code:
public class MainClass {
public static void main(String[] args) {
CountDownLatch latch1 = new CountDownLatch(1);
Thread t1 = new Thread(new AscendingOrder(latch1));
Thread t2 = new Thread(new DescendingOrder(latch1));
t1.start();
t2.start();
}
static class AscendingOrder implements Runnable {
private CountDownLatch latch;
public AscendingOrder(CountDownLatch latch) {
// TODO Auto-generated constructor stub
this.latch = latch;
}
public void run() {
// TODO Auto-generated method stub
System.out.println("thread t1 started.");
for (int i = 0; i < 10; i++)
System.out.println(i);
this.latch.countDown();
}
}
static class DescendingOrder implements Runnable {
private CountDownLatch latch;
public DescendingOrder(CountDownLatch latch1) {
// TODO Auto-generated constructor stub
this.latch = latch1;
}
public void run() {
// TODO Auto-generated method stub
System.out.println("thread t2 started and waiting");
try {
this.latch.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 10; i > 0; i--)
System.out.println(i);
}
}
}
Upvotes: 1
Views: 786
Reputation: 21883
1-how to do so using CountDownLatch and cyclic barrier?
CyclicBarrier is not ideal for this problem, I have written post comparing the two please have a look here
public class CountDownLatchTest {
public static void main(String[] args) {
CountDownLatch first = new CountDownLatch(1);
CountDownLatch prev = first;
for(int i=0;i<10;i++) {
CountDownLatch next = new CountDownLatch(1);
new TestThread(prev, next, i).start();
prev = next;
}
first.countDown();
}
public static class TestThread extends Thread {
private CountDownLatch prev;
private CountDownLatch next;
private int id;
public TestThread(CountDownLatch prev, CountDownLatch next, int id) {
this.prev = prev;
this.next = next;
this.id = id;
}
public void run() {
if (prev != null) {
try {
prev.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
doWork();
} finally {
if (next != null) {
next.countDown();
}
}
}
public void doWork() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Work done in Thread : "
+ id);
}
}
}
2- is the countDown parameter passed to the constructor of the CountDownLatch class should represents the number of threads waiting?
CountDownLatch.await() will wait until CountDownLatch.countDown() method is called countDown parameter times.
Upvotes: 0