Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfe20d0c

Browse files
committed
StampedLock examples
1 parent83db082 commitfe20d0c

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+
packagecom.winterbe.java8.samples.concurrent;
2+
3+
importjava.util.concurrent.ExecutorService;
4+
importjava.util.concurrent.Executors;
5+
importjava.util.concurrent.TimeUnit;
6+
importjava.util.concurrent.locks.StampedLock;
7+
8+
/**
9+
* @author Benjamin Winterberg
10+
*/
11+
publicclassLock4 {
12+
13+
privatestaticintcount =0;
14+
15+
publicstaticvoidmain(String[]args) {
16+
ExecutorServiceexecutor =Executors.newFixedThreadPool(2);
17+
18+
StampedLocklock =newStampedLock();
19+
20+
executor.submit(() -> {
21+
longstamp =lock.writeLock();
22+
try {
23+
count++;
24+
TimeUnit.SECONDS.sleep(1);
25+
}catch (InterruptedExceptione) {
26+
thrownewIllegalStateException(e);
27+
}finally {
28+
lock.unlockWrite(stamp);
29+
}
30+
});
31+
32+
executor.submit(() -> {
33+
longstamp =lock.readLock();
34+
try {
35+
System.out.println(Thread.currentThread().getName() +": " +count);
36+
TimeUnit.SECONDS.sleep(1);
37+
}catch (InterruptedExceptione) {
38+
thrownewIllegalStateException(e);
39+
}finally {
40+
lock.unlockRead(stamp);
41+
}
42+
});
43+
44+
executor.submit(() -> {
45+
longstamp =lock.readLock();
46+
try {
47+
System.out.println(Thread.currentThread().getName() +": " +count);
48+
TimeUnit.SECONDS.sleep(1);
49+
}catch (InterruptedExceptione) {
50+
thrownewIllegalStateException(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+
packagecom.winterbe.java8.samples.concurrent;
2+
3+
importjava.util.concurrent.ExecutorService;
4+
importjava.util.concurrent.Executors;
5+
importjava.util.concurrent.TimeUnit;
6+
importjava.util.concurrent.locks.StampedLock;
7+
8+
/**
9+
* @author Benjamin Winterberg
10+
*/
11+
publicclassLock5 {
12+
13+
privatestaticintcount =0;
14+
15+
publicstaticvoidmain(String[]args) {
16+
ExecutorServiceexecutor =Executors.newFixedThreadPool(2);
17+
18+
StampedLocklock =newStampedLock();
19+
20+
executor.submit(() -> {
21+
longstamp =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 (InterruptedExceptione) {
27+
thrownewIllegalStateException(e);
28+
}finally {
29+
lock.unlock(stamp);
30+
}
31+
});
32+
33+
executor.submit(() -> {
34+
longstamp =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+
privatestaticvoidincrementAndSleep(intsleepSeconds) {
49+
try {
50+
count++;
51+
TimeUnit.SECONDS.sleep(sleepSeconds);
52+
}catch (InterruptedExceptione) {
53+
thrownewIllegalStateException(e);
54+
}
55+
}
56+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packagecom.winterbe.java8.samples.concurrent;
2+
3+
importjava.util.concurrent.ExecutorService;
4+
importjava.util.concurrent.Executors;
5+
importjava.util.concurrent.locks.StampedLock;
6+
7+
/**
8+
* @author Benjamin Winterberg
9+
*/
10+
publicclassLock6 {
11+
12+
privatestaticintcount =0;
13+
14+
publicstaticvoidmain(String[]args) {
15+
ExecutorServiceexecutor =Executors.newFixedThreadPool(2);
16+
17+
StampedLocklock =newStampedLock();
18+
19+
executor.submit(() -> {
20+
longstamp =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)

[8]ページ先頭

©2009-2025 Movatter.jp