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

Commitf61cd1b

Browse files
committed
Simplify code
1 parente422b65 commitf61cd1b

File tree

6 files changed

+17
-37
lines changed

6 files changed

+17
-37
lines changed

‎src/com/winterbe/java8/samples/concurrent/ConcurrentUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ public static void stop(ExecutorService executor) {
2424
}
2525
}
2626

27+
publicstaticvoidsleep(intseconds) {
28+
try {
29+
TimeUnit.SECONDS.sleep(seconds);
30+
}catch (InterruptedExceptione) {
31+
thrownewIllegalStateException(e);
32+
}
33+
}
34+
2735
}

‎src/com/winterbe/java8/samples/concurrent/Lock1.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ private static void increment() {
2020
lock.lock();
2121
try {
2222
count++;
23-
}
24-
finally {
23+
}finally {
2524
lock.unlock();
2625
}
2726
}
@@ -36,7 +35,7 @@ private static void testLock() {
3635
ExecutorServiceexecutor =Executors.newFixedThreadPool(2);
3736

3837
IntStream.range(0,NUM_INCREMENTS)
39-
.forEach(i ->executor.submit(Lock1::increment));
38+
.forEach(i ->executor.submit(Lock1::increment));
4039

4140
ConcurrentUtils.stop(executor);
4241

‎src/com/winterbe/java8/samples/concurrent/Lock2.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
importjava.util.concurrent.ExecutorService;
44
importjava.util.concurrent.Executors;
5-
importjava.util.concurrent.TimeUnit;
65
importjava.util.concurrent.locks.ReentrantLock;
76

87
/**
@@ -18,9 +17,7 @@ public static void main(String[] args) {
1817
executor.submit(() -> {
1918
lock.lock();
2019
try {
21-
TimeUnit.SECONDS.sleep(1);
22-
}catch (InterruptedExceptione) {
23-
thrownewIllegalStateException(e);
20+
ConcurrentUtils.sleep(1);
2421
}finally {
2522
lock.unlock();
2623
}

‎src/com/winterbe/java8/samples/concurrent/Lock3.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
importjava.util.Map;
55
importjava.util.concurrent.ExecutorService;
66
importjava.util.concurrent.Executors;
7-
importjava.util.concurrent.TimeUnit;
87
importjava.util.concurrent.locks.ReadWriteLock;
98
importjava.util.concurrent.locks.ReentrantReadWriteLock;
109

@@ -23,10 +22,8 @@ public static void main(String[] args) {
2322
executor.submit(() -> {
2423
lock.writeLock().lock();
2524
try {
26-
TimeUnit.SECONDS.sleep(1);
25+
ConcurrentUtils.sleep(1);
2726
map.put("foo","bar");
28-
}catch (InterruptedExceptione) {
29-
thrownewIllegalStateException(e);
3027
}finally {
3128
lock.writeLock().unlock();
3229
}
@@ -36,9 +33,7 @@ public static void main(String[] args) {
3633
lock.readLock().lock();
3734
try {
3835
System.out.println(map.get("foo"));
39-
TimeUnit.SECONDS.sleep(1);
40-
}catch (InterruptedExceptione) {
41-
thrownewIllegalStateException(e);
36+
ConcurrentUtils.sleep(1);
4237
}finally {
4338
lock.readLock().unlock();
4439
}

‎src/com/winterbe/java8/samples/concurrent/Lock4.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
importjava.util.Map;
55
importjava.util.concurrent.ExecutorService;
66
importjava.util.concurrent.Executors;
7-
importjava.util.concurrent.TimeUnit;
87
importjava.util.concurrent.locks.StampedLock;
98

109
/**
@@ -22,10 +21,8 @@ public static void main(String[] args) {
2221
executor.submit(() -> {
2322
longstamp =lock.writeLock();
2423
try {
25-
TimeUnit.SECONDS.sleep(1);
24+
ConcurrentUtils.sleep(1);
2625
map.put("foo","bar");
27-
}catch (InterruptedExceptione) {
28-
thrownewIllegalStateException(e);
2926
}finally {
3027
lock.unlockWrite(stamp);
3128
}
@@ -35,9 +32,7 @@ public static void main(String[] args) {
3532
longstamp =lock.readLock();
3633
try {
3734
System.out.println(map.get("foo"));
38-
TimeUnit.SECONDS.sleep(1);
39-
}catch (InterruptedExceptione) {
40-
thrownewIllegalStateException(e);
35+
ConcurrentUtils.sleep(1);
4136
}finally {
4237
lock.unlockRead(stamp);
4338
}

‎src/com/winterbe/java8/samples/concurrent/Lock5.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
importjava.util.concurrent.ExecutorService;
44
importjava.util.concurrent.Executors;
5-
importjava.util.concurrent.TimeUnit;
65
importjava.util.concurrent.locks.StampedLock;
76

87
/**
98
* @author Benjamin Winterberg
109
*/
1110
publicclassLock5 {
1211

13-
privatestaticintcount =0;
14-
1512
publicstaticvoidmain(String[]args) {
1613
ExecutorServiceexecutor =Executors.newFixedThreadPool(2);
1714

@@ -21,10 +18,8 @@ public static void main(String[] args) {
2118
longstamp =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 (InterruptedExceptione) {
27-
thrownewIllegalStateException(e);
2823
}finally {
2924
lock.unlock(stamp);
3025
}
@@ -34,23 +29,14 @@ public static void main(String[] args) {
3429
longstamp =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-
privatestaticvoidincrementAndSleep(intsleepSeconds) {
49-
try {
50-
count++;
51-
TimeUnit.SECONDS.sleep(sleepSeconds);
52-
}catch (InterruptedExceptione) {
53-
thrownewIllegalStateException(e);
54-
}
55-
}
5642
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp