Skip to content

Commit e422b65

Browse files
committed
Simplify code
1 parent a636658 commit e422b65

File tree

3 files changed

+21
-50
lines changed

3 files changed

+21
-50
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ public static void main(String[] args) {
1818
executor.submit(() -> {
1919
lock.lock();
2020
try {
21-
System.out.println(lock.isLocked());
2221
TimeUnit.SECONDS.sleep(1);
23-
}
24-
catch (InterruptedException e) {
22+
} catch (InterruptedException e) {
2523
throw new IllegalStateException(e);
26-
}
27-
finally {
24+
} finally {
2825
lock.unlock();
2926
}
3027
});

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,26 @@ public static void main(String[] args) {
2525
try {
2626
TimeUnit.SECONDS.sleep(1);
2727
map.put("foo", "bar");
28-
}
29-
catch (InterruptedException e) {
28+
} catch (InterruptedException e) {
3029
throw new IllegalStateException(e);
31-
}
32-
finally {
30+
} finally {
3331
lock.writeLock().unlock();
3432
}
3533
});
3634

37-
executor.submit(() -> {
35+
Runnable readTask = () -> {
3836
lock.readLock().lock();
3937
try {
40-
String foo = map.get("foo");
41-
System.out.println(foo);
38+
System.out.println(map.get("foo"));
4239
TimeUnit.SECONDS.sleep(1);
43-
}
44-
catch (InterruptedException e) {
40+
} catch (InterruptedException e) {
4541
throw new IllegalStateException(e);
46-
}
47-
finally {
42+
} finally {
4843
lock.readLock().unlock();
4944
}
50-
});
51-
52-
executor.submit(() -> {
53-
lock.readLock().lock();
54-
try {
55-
String foo = map.get("foo");
56-
System.out.println(foo);
57-
TimeUnit.SECONDS.sleep(1);
58-
}
59-
catch (InterruptedException e) {
60-
throw new IllegalStateException(e);
61-
}
62-
finally {
63-
lock.readLock().unlock();
64-
}
65-
});
45+
};
46+
executor.submit(readTask);
47+
executor.submit(readTask);
6648

6749
ConcurrentUtils.stop(executor);
6850
}

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.winterbe.java8.samples.concurrent;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
35
import java.util.concurrent.ExecutorService;
46
import java.util.concurrent.Executors;
57
import java.util.concurrent.TimeUnit;
@@ -10,48 +12,38 @@
1012
*/
1113
public class Lock4 {
1214

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

18+
Map<String, String> map = new HashMap<>();
19+
1820
StampedLock lock = new StampedLock();
1921

2022
executor.submit(() -> {
2123
long stamp = lock.writeLock();
2224
try {
23-
count++;
2425
TimeUnit.SECONDS.sleep(1);
26+
map.put("foo", "bar");
2527
} catch (InterruptedException e) {
2628
throw new IllegalStateException(e);
2729
} finally {
2830
lock.unlockWrite(stamp);
2931
}
3032
});
3133

32-
executor.submit(() -> {
34+
Runnable readTask = () -> {
3335
long stamp = lock.readLock();
3436
try {
35-
System.out.println(Thread.currentThread().getName() + ": " + count);
37+
System.out.println(map.get("foo"));
3638
TimeUnit.SECONDS.sleep(1);
3739
} catch (InterruptedException e) {
3840
throw new IllegalStateException(e);
3941
} finally {
4042
lock.unlockRead(stamp);
4143
}
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-
});
44+
};
45+
executor.submit(readTask);
46+
executor.submit(readTask);
5547

5648
ConcurrentUtils.stop(executor);
5749
}

0 commit comments

Comments
 (0)