Skip to content

Commit a636658

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 4a8a7e0 + fe20d0c commit a636658

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)