Skip to content

Commit a765564

Browse files
CountDownLatch and CyclicBarrier example
1 parent 337e11c commit a765564

5 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package tr.salkan.code.java.threadExecutor.example.threads.countDownLatch;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
public class Example {
6+
7+
public static void main(String[] args) throws InterruptedException {
8+
9+
CountDownLatch latch = new CountDownLatch(4);
10+
11+
ThreadExample first = new ThreadExample(1000, latch,
12+
"ThreadExample-1");
13+
ThreadExample second = new ThreadExample(2000, latch,
14+
"ThreadExample-2");
15+
ThreadExample third = new ThreadExample(3000, latch,
16+
"ThreadExample-3");
17+
ThreadExample fourth = new ThreadExample(4000, latch,
18+
"ThreadExample-4");
19+
20+
fourth.start();
21+
third.start();
22+
second.start();
23+
first.start();
24+
25+
latch.await();
26+
27+
System.out.println(Thread.currentThread().getName() +
28+
" has finished");
29+
}
30+
}
31+
32+
class ThreadExample extends Thread
33+
{
34+
private int delay;
35+
private CountDownLatch latch;
36+
37+
public ThreadExample(int delay, CountDownLatch latch,
38+
String name)
39+
{
40+
super(name);
41+
this.delay = delay;
42+
this.latch = latch;
43+
}
44+
45+
@Override
46+
public void run()
47+
{
48+
try
49+
{
50+
Thread.sleep(delay);
51+
latch.countDown();
52+
System.out.println(Thread.currentThread().getName()
53+
+ " finished");
54+
}
55+
catch (InterruptedException e)
56+
{
57+
e.printStackTrace();
58+
}
59+
}
60+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CountDownLatch -> Java 1.5
2+
3+
But using
4+
5+
CompletableFuture
6+
7+
**** CompletableFuture.allOf(future1,future2)
8+
9+
or
10+
11+
future1.thenAcceptBothAsync(future2, (x,y) -> System.out.println("Result = " + (x + y))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package tr.salkan.code.java.threadExecutor.example.threads.cyclicBarrier;
2+
3+
public class CyclicBarrierFinishEvent implements Runnable{
4+
5+
public void run() {
6+
7+
System.out.println("As 3 threads have reached common barrier point "
8+
+ ", CyclicBarrrierFinishEvent has been triggered");
9+
System.out.println("You can update shared variables if any");
10+
}
11+
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tr.salkan.code.java.threadExecutor.example.threads.cyclicBarrier;
2+
3+
import java.util.concurrent.CyclicBarrier;
4+
5+
public class Example {
6+
7+
public static void main(String[] args) {
8+
9+
CyclicBarrier cyclicBarrier=new CyclicBarrier(3 ,new CyclicBarrierFinishEvent());
10+
11+
RunnableTaskExample runnableTask1=new RunnableTaskExample(cyclicBarrier,1000);
12+
RunnableTaskExample runnableTask2=new RunnableTaskExample(cyclicBarrier,2000);
13+
RunnableTaskExample runnableTask3=new RunnableTaskExample(cyclicBarrier,3000);
14+
15+
16+
//Create and start 3 threads
17+
new Thread(runnableTask1,"Thread-1").start();
18+
new Thread(runnableTask2,"Thread-2").start();
19+
new Thread(runnableTask3,"Thread-3").start();
20+
21+
/*
22+
* We are reusing cyclic barrier using below threads
23+
* */
24+
RunnableTaskExample runnableTask4=new RunnableTaskExample(cyclicBarrier,4000);
25+
RunnableTaskExample runnableTask5=new RunnableTaskExample(cyclicBarrier,5000);
26+
RunnableTaskExample runnableTask6=new RunnableTaskExample(cyclicBarrier,6000);
27+
28+
// Create and start 3 more threads
29+
new Thread(runnableTask4,"Thread-4").start();
30+
new Thread(runnableTask5,"Thread-5").start();
31+
new Thread(runnableTask6,"Thread-6").start();
32+
33+
34+
}
35+
}
36+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tr.salkan.code.java.threadExecutor.example.threads.cyclicBarrier;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
6+
public class RunnableTaskExample implements Runnable{
7+
8+
CyclicBarrier cyclicBarrier;
9+
long sleepTime;
10+
11+
RunnableTaskExample(CyclicBarrier cyclicBarrier,long sleepTime){
12+
this.cyclicBarrier=cyclicBarrier;
13+
this.sleepTime=sleepTime;
14+
}
15+
16+
@Override
17+
public void run() {
18+
19+
try {
20+
Thread.sleep(sleepTime);
21+
System.out.println(Thread.currentThread().getName() +
22+
" is waiting for "+(cyclicBarrier.getParties()-cyclicBarrier.getNumberWaiting()-1)+
23+
" other threads to reach common barrier point");
24+
25+
cyclicBarrier.await();
26+
} catch (InterruptedException e) {
27+
e.printStackTrace();
28+
} catch (BrokenBarrierException e) {
29+
e.printStackTrace();
30+
}
31+
32+
System.out.println("As "+cyclicBarrier.getParties()+ " threads have reached common barrier point "
33+
+ Thread.currentThread().getName() +
34+
" has been released");
35+
}
36+
37+
}

0 commit comments

Comments
 (0)