File tree 1 file changed +14
-1
lines changed
src/com/winterbe/java8/samples/concurrent
1 file changed +14
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import java .util .concurrent .ExecutorService ;
4
4
import java .util .concurrent .Executors ;
5
+ import java .util .concurrent .TimeUnit ;
5
6
import java .util .concurrent .locks .ReentrantLock ;
6
7
7
8
/**
@@ -18,11 +19,23 @@ public static void main(String[] args) {
18
19
lock .lock ();
19
20
try {
20
21
System .out .println (lock .isLocked ());
21
- } finally {
22
+ TimeUnit .SECONDS .sleep (1 );
23
+ }
24
+ catch (InterruptedException e ) {
25
+ throw new IllegalStateException (e );
26
+ }
27
+ finally {
22
28
lock .unlock ();
23
29
}
24
30
});
25
31
32
+ executor .submit (() -> {
33
+ System .out .println ("Locked: " + lock .isLocked ());
34
+ System .out .println ("Held by me: " + lock .isHeldByCurrentThread ());
35
+ boolean locked = lock .tryLock ();
36
+ System .out .println ("Lock acquired: " + locked );
37
+ });
38
+
26
39
ConcurrentUtils .stop (executor );
27
40
}
28
41
You can’t perform that action at this time.
0 commit comments