22
33import java .util .concurrent .ExecutorService ;
44import java .util .concurrent .Executors ;
5- import java .util .concurrent .TimeUnit ;
65import java .util .concurrent .locks .StampedLock ;
76
87/**
98 * @author Benjamin Winterberg
109 */
1110public class Lock5 {
1211
13- private static int count = 0 ;
14-
1512 public static void main (String [] args ) {
1613 ExecutorService executor = Executors .newFixedThreadPool (2 );
1714
@@ -21,10 +18,8 @@ public static void main(String[] args) {
2118 long stamp = lock .tryOptimisticRead ();
2219 try {
2320 System .out .println ("Optimistic Lock Valid: " + lock .validate (stamp ));
24- TimeUnit . SECONDS .sleep (1 );
21+ ConcurrentUtils .sleep (1 );
2522 System .out .println ("Optimistic Lock Valid: " + lock .validate (stamp ));
26- } catch (InterruptedException e ) {
27- throw new IllegalStateException (e );
2823 } finally {
2924 lock .unlock (stamp );
3025 }
@@ -34,23 +29,14 @@ public static void main(String[] args) {
3429 long stamp = lock .writeLock ();
3530 try {
3631 System .out .println ("Write Lock acquired" );
37- incrementAndSleep (2 );
32+ ConcurrentUtils . sleep (2 );
3833 } finally {
3934 lock .unlock (stamp );
4035 System .out .println ("Write done" );
4136 }
4237 });
4338
44-
4539 ConcurrentUtils .stop (executor );
4640 }
4741
48- private static void incrementAndSleep (int sleepSeconds ) {
49- try {
50- count ++;
51- TimeUnit .SECONDS .sleep (sleepSeconds );
52- } catch (InterruptedException e ) {
53- throw new IllegalStateException (e );
54- }
55- }
5642}
0 commit comments