Skip to content

Commit f61cd1b

Browse files
committed
Simplify code
1 parent e422b65 commit f61cd1b

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+
public static void sleep(int seconds) {
28+
try {
29+
TimeUnit.SECONDS.sleep(seconds);
30+
} catch (InterruptedException e) {
31+
throw new IllegalStateException(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
ExecutorService executor = 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
import java.util.concurrent.ExecutorService;
44
import java.util.concurrent.Executors;
5-
import java.util.concurrent.TimeUnit;
65
import java.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 (InterruptedException e) {
23-
throw new IllegalStateException(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
import java.util.Map;
55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
7-
import java.util.concurrent.TimeUnit;
87
import java.util.concurrent.locks.ReadWriteLock;
98
import java.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 (InterruptedException e) {
29-
throw new IllegalStateException(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 (InterruptedException e) {
41-
throw new IllegalStateException(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
import java.util.Map;
55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
7-
import java.util.concurrent.TimeUnit;
87
import java.util.concurrent.locks.StampedLock;
98

109
/**
@@ -22,10 +21,8 @@ public static void main(String[] args) {
2221
executor.submit(() -> {
2322
long stamp = lock.writeLock();
2423
try {
25-
TimeUnit.SECONDS.sleep(1);
24+
ConcurrentUtils.sleep(1);
2625
map.put("foo", "bar");
27-
} catch (InterruptedException e) {
28-
throw new IllegalStateException(e);
2926
} finally {
3027
lock.unlockWrite(stamp);
3128
}
@@ -35,9 +32,7 @@ public static void main(String[] args) {
3532
long stamp = lock.readLock();
3633
try {
3734
System.out.println(map.get("foo"));
38-
TimeUnit.SECONDS.sleep(1);
39-
} catch (InterruptedException e) {
40-
throw new IllegalStateException(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
import java.util.concurrent.ExecutorService;
44
import java.util.concurrent.Executors;
5-
import java.util.concurrent.TimeUnit;
65
import java.util.concurrent.locks.StampedLock;
76

87
/**
98
* @author Benjamin Winterberg
109
*/
1110
public class Lock5 {
1211

13-
private static int count = 0;
14-
1512
public static void main(String[] args) {
1613
ExecutorService executor = Executors.newFixedThreadPool(2);
1714

@@ -21,10 +18,8 @@ public static void main(String[] args) {
2118
long stamp = 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 (InterruptedException e) {
27-
throw new IllegalStateException(e);
2823
} finally {
2924
lock.unlock(stamp);
3025
}
@@ -34,23 +29,14 @@ public static void main(String[] args) {
3429
long stamp = 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-
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-
}
5642
}

0 commit comments

Comments
 (0)