@@ -20,37 +20,24 @@ public static final void main(final String... args) {
20
20
/* Create new instance of lock test */
21
21
final SemaphoreTest lockTest = new SemaphoreTest ();
22
22
23
- /* Start first anonymous thread */
24
- new Thread ("First-Thread" ) {
23
+ for (int i = 0 ; i < 100 ; i ++) {
25
24
26
- @ Override
27
- public void run ( ) {
25
+ /* Spawn several anonymous thread */
26
+ new Thread ( "Thread-" + i ) {
28
27
29
- if (SafetyMode .SAFE .equals (_MODE )) {
28
+ @ Override
29
+ public void run () {
30
30
31
- lockTest .iAmThreadSafe ();
32
- } else {
31
+ if (SafetyMode .SAFE .equals (_MODE )) {
33
32
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 {
44
35
45
- if (SafetyMode .SAFE .equals (_MODE )) {
46
-
47
- lockTest .iAmThreadSafe ();
48
- } else {
49
-
50
- lockTest .iAmNotThreadSafe ();
36
+ lockTest .iAmNotThreadSafe ();
37
+ }
51
38
}
52
- }
53
- }. start ();
39
+ }. start ();
40
+ }
54
41
}
55
42
56
43
/**
@@ -66,15 +53,15 @@ private final void iAmNotThreadSafe() {
66
53
try {
67
54
68
55
/* 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 () );
70
57
71
58
/* Sleep this thread so that another thread can do the same operation */
72
59
Thread .sleep (1000 );
73
60
74
61
} catch (final InterruptedException e ) {
75
62
e .printStackTrace ();
76
63
} 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 () );
78
65
}
79
66
}
80
67
@@ -94,7 +81,7 @@ private final void iAmThreadSafe() {
94
81
lock .acquire ();
95
82
96
83
/* 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 () );
98
85
99
86
/* Sleep this thread so that another thread can do the same operation */
100
87
Thread .sleep (1000 );
@@ -105,7 +92,7 @@ private final void iAmThreadSafe() {
105
92
106
93
/* Release the lock */
107
94
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 () );
109
96
}
110
97
}
111
98
0 commit comments