| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,13 +18,10 @@ public static void main(String[] args) { | |||
| 18 | 18 | executor.submit(() -> { | |
| 19 | 19 | lock.lock(); | |
| 20 | 20 | try { | |
| 21 | - System.out.println(lock.isLocked()); | ||
| 22 | 21 | TimeUnit.SECONDS.sleep(1); | |
| 23 | - } | ||
| 24 | - catch (InterruptedException e) { | ||
| 22 | + } catch (InterruptedException e) { | ||
| 25 | 23 | throw new IllegalStateException(e); | |
| 26 | - } | ||
| 27 | - finally { | ||
| 24 | + } finally { | ||
| 28 | 25 | lock.unlock(); | |
| 29 | 26 | } | |
| 30 | 27 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,44 +25,26 @@ public static void main(String[] args) { | |||
| 25 | 25 | try { | |
| 26 | 26 | TimeUnit.SECONDS.sleep(1); | |
| 27 | 27 | map.put("foo", "bar"); | |
| 28 | - } | ||
| 29 | - catch (InterruptedException e) { | ||
| 28 | + } catch (InterruptedException e) { | ||
| 30 | 29 | throw new IllegalStateException(e); | |
| 31 | - } | ||
| 32 | - finally { | ||
| 30 | + } finally { | ||
| 33 | 31 | lock.writeLock().unlock(); | |
| 34 | 32 | } | |
| 35 | 33 | }); | |
| 36 | 34 | ||
| 37 | - executor.submit(() -> { | ||
| 35 | + Runnable readTask = () -> { | ||
| 38 | 36 | lock.readLock().lock(); | |
| 39 | 37 | try { | |
| 40 | - String foo = map.get("foo"); | ||
| 41 | - System.out.println(foo); | ||
| 38 | + System.out.println(map.get("foo")); | ||
| 42 | 39 | TimeUnit.SECONDS.sleep(1); | |
| 43 | - } | ||
| 44 | - catch (InterruptedException e) { | ||
| 40 | + } catch (InterruptedException e) { | ||
| 45 | 41 | throw new IllegalStateException(e); | |
| 46 | - } | ||
| 47 | - finally { | ||
| 42 | + } finally { | ||
| 48 | 43 | lock.readLock().unlock(); | |
| 49 | 44 | } | |
| 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); | ||
| 66 | 48 | ||
| 67 | 49 | ConcurrentUtils.stop(executor); | |
| 68 | 50 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,7 @@ | |||
| 1 | 1 | package com.winterbe.java8.samples.concurrent; | |
| 2 | 2 | ||
| 3 | + import java.util.HashMap; | ||
| 4 | + import java.util.Map; | ||
| 3 | 5 | import java.util.concurrent.ExecutorService; | |
| 4 | 6 | import java.util.concurrent.Executors; | |
| 5 | 7 | import java.util.concurrent.TimeUnit; | |
@@ -10,48 +12,38 @@ | |||
| 10 | 12 | */ | |
| 11 | 13 | public class Lock4 { | |
| 12 | 14 | ||
| 13 | - private static int count = 0; | ||
| 14 | - | ||
| 15 | 15 | public static void main(String[] args) { | |
| 16 | 16 | ExecutorService executor = Executors.newFixedThreadPool(2); | |
| 17 | 17 | ||
| 18 | + Map<String, String> map = new HashMap<>(); | ||
| 19 | + | ||
| 18 | 20 | StampedLock lock = new StampedLock(); | |
| 19 | 21 | ||
| 20 | 22 | executor.submit(() -> { | |
| 21 | 23 | long stamp = lock.writeLock(); | |
| 22 | 24 | try { | |
| 23 | - count++; | ||
| 24 | 25 | TimeUnit.SECONDS.sleep(1); | |
| 26 | + map.put("foo", "bar"); | ||
| 25 | 27 | } catch (InterruptedException e) { | |
| 26 | 28 | throw new IllegalStateException(e); | |
| 27 | 29 | } finally { | |
| 28 | 30 | lock.unlockWrite(stamp); | |
| 29 | 31 | } | |
| 30 | 32 | }); | |
| 31 | 33 | ||
| 32 | - executor.submit(() -> { | ||
| 34 | + Runnable readTask = () -> { | ||
| 33 | 35 | long stamp = lock.readLock(); | |
| 34 | 36 | try { | |
| 35 | - System.out.println(Thread.currentThread().getName() + ": " + count); | ||
| 37 | + System.out.println(map.get("foo")); | ||
| 36 | 38 | TimeUnit.SECONDS.sleep(1); | |
| 37 | 39 | } catch (InterruptedException e) { | |
| 38 | 40 | throw new IllegalStateException(e); | |
| 39 | 41 | } finally { | |
| 40 | 42 | lock.unlockRead(stamp); | |
| 41 | 43 | } | |
| 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); | ||
| 55 | 47 | ||
| 56 | 48 | ConcurrentUtils.stop(executor); | |
| 57 | 49 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments