File tree Expand file tree Collapse file tree 3 files changed +154
-0
lines changed
src/com/winterbe/java8/samples/concurrent Expand file tree Collapse file tree 3 files changed +154
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .winterbe .java8 .samples .concurrent ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .concurrent .TimeUnit ;
6+ import java .util .concurrent .locks .StampedLock ;
7+
8+ /**
9+ * @author Benjamin Winterberg
10+ */
11+ public class Lock4 {
12+
13+ private static int count = 0 ;
14+
15+ public static void main (String [] args ){
16+ ExecutorService executor = Executors .newFixedThreadPool (2 );
17+
18+ StampedLock lock = new StampedLock ();
19+
20+ executor .submit (() ->{
21+ long stamp = lock .writeLock ();
22+ try {
23+ count ++;
24+ TimeUnit .SECONDS .sleep (1 );
25+ } catch (InterruptedException e ){
26+ throw new IllegalStateException (e );
27+ } finally {
28+ lock .unlockWrite (stamp );
29+ }
30+ });
31+
32+ executor .submit (() ->{
33+ long stamp = lock .readLock ();
34+ try {
35+ System .out .println (Thread .currentThread ().getName () + ": " + count );
36+ TimeUnit .SECONDS .sleep (1 );
37+ } catch (InterruptedException e ){
38+ throw new IllegalStateException (e );
39+ } finally {
40+ lock .unlockRead (stamp );
41+ }
42+ });
43+
44+ executor .submit (() ->{
45+ long stamp = lock .readLock ();
46+ try {
47+ System .out .println (Thread .currentThread ().getName () + ": " + count );
48+ TimeUnit .SECONDS .sleep (1 );
49+ } catch (InterruptedException e ){
50+ throw new IllegalStateException (e );
51+ } finally {
52+ lock .unlockRead (stamp );
53+ }
54+ });
55+
56+ ConcurrentUtils .stop (executor );
57+ }
58+
59+ }
Original file line number Diff line number Diff line change 1+ package com .winterbe .java8 .samples .concurrent ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .concurrent .TimeUnit ;
6+ import java .util .concurrent .locks .StampedLock ;
7+
8+ /**
9+ * @author Benjamin Winterberg
10+ */
11+ public class Lock5 {
12+
13+ private static int count = 0 ;
14+
15+ public static void main (String [] args ){
16+ ExecutorService executor = Executors .newFixedThreadPool (2 );
17+
18+ StampedLock lock = new StampedLock ();
19+
20+ executor .submit (() ->{
21+ long stamp = lock .tryOptimisticRead ();
22+ try {
23+ System .out .println ("Optimistic Lock Valid: " + lock .validate (stamp ));
24+ TimeUnit .SECONDS .sleep (1 );
25+ System .out .println ("Optimistic Lock Valid: " + lock .validate (stamp ));
26+ } catch (InterruptedException e ){
27+ throw new IllegalStateException (e );
28+ } finally {
29+ lock .unlock (stamp );
30+ }
31+ });
32+
33+ executor .submit (() ->{
34+ long stamp = lock .writeLock ();
35+ try {
36+ System .out .println ("Write Lock acquired" );
37+ incrementAndSleep (2 );
38+ } finally {
39+ lock .unlock (stamp );
40+ System .out .println ("Write done" );
41+ }
42+ });
43+
44+
45+ ConcurrentUtils .stop (executor );
46+ }
47+
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+ }
56+ }
Original file line number Diff line number Diff line change 1+ package com .winterbe .java8 .samples .concurrent ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .concurrent .locks .StampedLock ;
6+
7+ /**
8+ * @author Benjamin Winterberg
9+ */
10+ public class Lock6 {
11+
12+ private static int count = 0 ;
13+
14+ public static void main (String [] args ){
15+ ExecutorService executor = Executors .newFixedThreadPool (2 );
16+
17+ StampedLock lock = new StampedLock ();
18+
19+ executor .submit (() ->{
20+ long stamp = lock .readLock ();
21+ try {
22+ if (count == 0 ){
23+ stamp = lock .tryConvertToWriteLock (stamp );
24+ if (stamp == 0L ){
25+ System .out .println ("Could not convert to write lock" );
26+ stamp = lock .writeLock ();
27+ }
28+ count = 23 ;
29+ }
30+ System .out .println (count );
31+ } finally {
32+ lock .unlock (stamp );
33+ }
34+ });
35+
36+ ConcurrentUtils .stop (executor );
37+ }
38+
39+ }
You can’t perform that action at this time.
0 commit comments