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

Commite422b65

Browse files
committed
Simplify code
1 parenta636658 commite422b65

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 (InterruptedExceptione) {
22+
}catch (InterruptedExceptione) {
2523
thrownewIllegalStateException(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 (InterruptedExceptione) {
28+
}catch (InterruptedExceptione) {
3029
thrownewIllegalStateException(e);
31-
}
32-
finally {
30+
}finally {
3331
lock.writeLock().unlock();
3432
}
3533
});
3634

37-
executor.submit(() -> {
35+
RunnablereadTask =() -> {
3836
lock.readLock().lock();
3937
try {
40-
Stringfoo =map.get("foo");
41-
System.out.println(foo);
38+
System.out.println(map.get("foo"));
4239
TimeUnit.SECONDS.sleep(1);
43-
}
44-
catch (InterruptedExceptione) {
40+
}catch (InterruptedExceptione) {
4541
thrownewIllegalStateException(e);
46-
}
47-
finally {
42+
}finally {
4843
lock.readLock().unlock();
4944
}
50-
});
51-
52-
executor.submit(() -> {
53-
lock.readLock().lock();
54-
try {
55-
Stringfoo =map.get("foo");
56-
System.out.println(foo);
57-
TimeUnit.SECONDS.sleep(1);
58-
}
59-
catch (InterruptedExceptione) {
60-
thrownewIllegalStateException(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
packagecom.winterbe.java8.samples.concurrent;
22

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

13-
privatestaticintcount =0;
14-
1515
publicstaticvoidmain(String[]args) {
1616
ExecutorServiceexecutor =Executors.newFixedThreadPool(2);
1717

18+
Map<String,String>map =newHashMap<>();
19+
1820
StampedLocklock =newStampedLock();
1921

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

32-
executor.submit(() -> {
34+
RunnablereadTask =() -> {
3335
longstamp =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 (InterruptedExceptione) {
3840
thrownewIllegalStateException(e);
3941
}finally {
4042
lock.unlockRead(stamp);
4143
}
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-
});
44+
};
45+
executor.submit(readTask);
46+
executor.submit(readTask);
5547

5648
ConcurrentUtils.stop(executor);
5749
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp