Skip to content

Commit 311648b

Browse files
committed
SemaphoreTest++
1 parent e06c116 commit 311648b

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

src/main/java/com/inbravo/concurrency/SemaphoreTest.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,24 @@ public static final void main(final String... args) {
2020
/* Create new instance of lock test */
2121
final SemaphoreTest lockTest = new SemaphoreTest();
2222

23-
/* Start first anonymous thread */
24-
new Thread("First-Thread") {
23+
for (int i = 0; i < 100; i++) {
2524

26-
@Override
27-
public void run() {
25+
/* Spawn several anonymous thread */
26+
new Thread("Thread-" + i) {
2827

29-
if (SafetyMode.SAFE.equals(_MODE)) {
28+
@Override
29+
public void run() {
3030

31-
lockTest.iAmThreadSafe();
32-
} else {
31+
if (SafetyMode.SAFE.equals(_MODE)) {
3332

34-
lockTest.iAmNotThreadSafe();
35-
}
36-
}
37-
}.start();
38-
39-
/* Start second anonymous thread */
40-
new Thread("Second-Thread") {
41-
42-
@Override
43-
public void run() {
33+
lockTest.iAmThreadSafe();
34+
} else {
4435

45-
if (SafetyMode.SAFE.equals(_MODE)) {
46-
47-
lockTest.iAmThreadSafe();
48-
} else {
49-
50-
lockTest.iAmNotThreadSafe();
36+
lockTest.iAmNotThreadSafe();
37+
}
5138
}
52-
}
53-
}.start();
39+
}.start();
40+
}
5441
}
5542

5643
/**
@@ -66,15 +53,15 @@ private final void iAmNotThreadSafe() {
6653
try {
6754

6855
/* Print current thread info */
69-
System.out.println(Thread.currentThread().getName() + " is inside critical section");
56+
System.out.println(Thread.currentThread().getName() + " is inside critical section at time : " + System.currentTimeMillis());
7057

7158
/* Sleep this thread so that another thread can do the same operation */
7259
Thread.sleep(1000);
7360

7461
} catch (final InterruptedException e) {
7562
e.printStackTrace();
7663
} finally {
77-
System.out.println(Thread.currentThread().getName() + " is out of critical section");
64+
System.out.println(Thread.currentThread().getName() + " is out of critical section at time : " + System.currentTimeMillis());
7865
}
7966
}
8067

@@ -94,7 +81,7 @@ private final void iAmThreadSafe() {
9481
lock.acquire();
9582

9683
/* Print current thread info */
97-
System.out.println(Thread.currentThread().getName() + " is inside critical section");
84+
System.out.println(Thread.currentThread().getName() + " is inside critical section at time : " + System.currentTimeMillis());
9885

9986
/* Sleep this thread so that another thread can do the same operation */
10087
Thread.sleep(1000);
@@ -105,7 +92,7 @@ private final void iAmThreadSafe() {
10592

10693
/* Release the lock */
10794
lock.release();
108-
System.out.println(Thread.currentThread().getName() + " is out of critical section");
95+
System.out.println(Thread.currentThread().getName() + " is out of critical section at time : " + System.currentTimeMillis());
10996
}
11097
}
11198

0 commit comments

Comments
 (0)