-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDown.java
More file actions
53 lines (39 loc) · 1.33 KB
/
CountDown.java
File metadata and controls
53 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package concurrentCollection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDown {
public static void main(String[] args) throws InterruptedException {
int chefs=10;
CountDownLatch latch=new CountDownLatch(chefs);
try(ExecutorService ex= Executors.newFixedThreadPool(chefs)){
for (int i = 0; i < 10; i++) {
ex.execute(new Chef("Dibyo","Pizza",latch));
Thread.sleep(1000);
}
}
latch.await();
System.out.println("All dishes ready..");
}
}
class Chef implements Runnable{
private final String name;
private final String dish;
private final CountDownLatch latch;
public Chef(String name, String dish, CountDownLatch latch){
this.dish=dish;
this.name=name;
this.latch=latch;
}
@Override
public void run(){
try{
System.out.println(name+" is preparing "+dish+" by "+Thread.currentThread().getName());
Thread.sleep(2000);
System.out.println(name+" has finished preparing "+dish+" by "+Thread.currentThread().getName());
latch.countDown();
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}