File tree Expand file tree Collapse file tree 1 file changed +8
-11
lines changed Expand file tree Collapse file tree 1 file changed +8
-11
lines changed Original file line number Diff line number Diff line change 1
- package main .concurrency ;
2
-
3
1
import java .util .concurrent .Semaphore ;
4
2
import java .util .concurrent .ThreadLocalRandom ;
5
3
8
6
* [1] Can be used by multiple threads at the same time and includes a counter to track availability
9
7
*/
10
8
public class SemaphoreDemo {
9
+ private static final Semaphore charger = new Semaphore (5 );
11
10
12
- static class ElectricalVehicle extends Thread {
11
+ public static void main (String [] args ) {
12
+ for (int i = 0 ; i < 10 ; i ++) {
13
+ new ElectricalVehicle ("EV" + i ).start ();
14
+ }
15
+ }
13
16
14
- static Semaphore charger = new Semaphore ( 5 );
15
- String name ;
17
+ private static class ElectricalVehicle extends Thread {
18
+ private final String name ;
16
19
17
20
ElectricalVehicle (String name ) {
18
21
this .name = name ;
@@ -31,10 +34,4 @@ public void run() {
31
34
}
32
35
}
33
36
}
34
-
35
- public static void main (String [] args ) {
36
- for (int i = 0 ; i < 10 ; i ++) {
37
- new ElectricalVehicle ("EV" + i ).start ();
38
- }
39
- }
40
37
}
You can’t perform that action at this time.
0 commit comments