-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatch.java
More file actions
39 lines (32 loc) · 1.2 KB
/
CountDownLatch.java
File metadata and controls
39 lines (32 loc) · 1.2 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
import java.util.concurrent.CountDownLatch;
public class CarService {
public static void main(String[] args) throws InterruptedException {
CountDownLatch carService = new CountDownLatch(3);
// Kitchen tasks for each course
new Thread(new CookingTask("OilChange", carService)).start();
new Thread(new CookingTask("TireRotation", carService)).start();
new Thread(new CookingTask("15-Point Inspection", carService)).start();
// Wait for all courses to be ready
carService.await();
System.out.println("Your oil change is complete.");
}
}
class CookingTask implements Runnable {
private final String course;
private final CountDownLatch latch;
public CookingTask(String course, CountDownLatch latch) {
this.course = course;
this.latch = latch;
}
@Override
public void run() {
// Simulate cooking time
try {
Thread.sleep((long) (Math.random() * 1000));
System.out.println(course + " is ready!");
latch.countDown(); // Signal that this course is ready
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}