-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSemaphoreDemo.java
37 lines (32 loc) · 1.13 KB
/
SemaphoreDemo.java
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
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;
/**
* Simple example of using Semaphore[1] to control access to shared resources
* [1] Can be used by multiple threads at the same time and includes a counter to track availability
*/
public class SemaphoreDemo {
private static final Semaphore charger = new Semaphore(5);
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new ElectricalVehicle("EV" + i).start();
}
}
private static class ElectricalVehicle extends Thread {
private final String name;
ElectricalVehicle(String name) {
this.name = name;
}
public void run() {
try {
charger.acquire();
System.out.println(name + " is charging..");
Thread.sleep(ThreadLocalRandom.current().nextInt(2000, 4000));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(name + " finished charging.");
charger.release();
}
}
}
}