Skip to content

Commit 282c9f3

Browse files
authored
Update SemaphoreDemo.java
1 parent 7984ee7 commit 282c9f3

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

SemaphoreDemo.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package main.concurrency;
2-
31
import java.util.concurrent.Semaphore;
42
import java.util.concurrent.ThreadLocalRandom;
53

@@ -8,11 +6,16 @@
86
* [1] Can be used by multiple threads at the same time and includes a counter to track availability
97
*/
108
public class SemaphoreDemo {
9+
private static final Semaphore charger = new Semaphore(5);
1110

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+
}
1316

14-
static Semaphore charger = new Semaphore(5);
15-
String name;
17+
private static class ElectricalVehicle extends Thread {
18+
private final String name;
1619

1720
ElectricalVehicle(String name) {
1821
this.name = name;
@@ -31,10 +34,4 @@ public void run() {
3134
}
3235
}
3336
}
34-
35-
public static void main(String[] args) {
36-
for (int i = 0; i < 10; i++) {
37-
new ElectricalVehicle("EV" + i).start();
38-
}
39-
}
4037
}

0 commit comments

Comments
 (0)