From 5065a7353dd2d4856c4bf8997d511a7c3ce6f650 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 15 Mar 2020 12:50:43 +0100 Subject: [PATCH] BAEL-3855 --- .../lock/ConcurrentAccessBenchmark.java | 11 ++++++----- .../com/baeldung/concurrent/lock/SingleLock.java | 12 ++---------- .../baeldung/concurrent/lock/StripedLock.java | 16 ++++------------ 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/ConcurrentAccessBenchmark.java b/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/ConcurrentAccessBenchmark.java index 2dcaa5cb53..ceb53ce077 100644 --- a/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/ConcurrentAccessBenchmark.java +++ b/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/ConcurrentAccessBenchmark.java @@ -18,36 +18,37 @@ import org.openjdk.jmh.annotations.Warmup; @Fork(value = 2) @Warmup(iterations = 0) public class ConcurrentAccessBenchmark { - ConcurrentAccessExperiment accessMyMap; static final int SLOTS = 4; static final int THREADS = 10000; static final int BUCKETS = Runtime.getRuntime().availableProcessors() * SLOTS; + SingleLock singleLock = new SingleLock(); + StripedLock stripedLock = new StripedLock(BUCKETS); @Benchmark @BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MILLISECONDS) public Map singleLockHashMap() throws InterruptedException { - return (new SingleLock()).doWork(new HashMap(), THREADS, SLOTS); + return singleLock.doWork(new HashMap(), THREADS, SLOTS); } @Benchmark @BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MILLISECONDS) public Map stripedLockHashMap() throws InterruptedException { - return (new StripedLock(BUCKETS)).doWork(new HashMap(), THREADS, SLOTS); + return stripedLock.doWork(new HashMap(), THREADS, SLOTS); } @Benchmark @BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MILLISECONDS) public Map singleLockConcurrentHashMap() throws InterruptedException { - return (new SingleLock()).doWork(new ConcurrentHashMap(), THREADS, SLOTS); + return singleLock.doWork(new ConcurrentHashMap(), THREADS, SLOTS); } @Benchmark @BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MILLISECONDS) public Map stripedLockConcurrentHashMap() throws InterruptedException { - return (new StripedLock(BUCKETS)).doWork(new ConcurrentHashMap(), THREADS, SLOTS); + return stripedLock.doWork(new ConcurrentHashMap(), THREADS, SLOTS); } } diff --git a/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/SingleLock.java b/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/SingleLock.java index 9bbac82c6d..762053c4c9 100644 --- a/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/SingleLock.java +++ b/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/SingleLock.java @@ -17,12 +17,8 @@ public class SingleLock extends ConcurrentAccessExperiment { try { lock.lock(); map.put("key" + key, "value" + key); - } catch (Exception e) { - this.putSupplier(map, key); } finally { - try { - lock.unlock(); - } catch (Exception e) {} + lock.unlock(); } return null; }); @@ -33,12 +29,8 @@ public class SingleLock extends ConcurrentAccessExperiment { try { lock.lock(); map.get("key" + key); - } catch (Exception e) { - this.getSupplier(map, key); } finally { - try { - lock.unlock(); - } catch (Exception e) {} + lock.unlock(); } return null; }); diff --git a/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/StripedLock.java b/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/StripedLock.java index 46610c3b78..e132cb061d 100644 --- a/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/StripedLock.java +++ b/core-java-modules/core-java-concurrency-collections-2/src/main/java/com/baeldung/concurrent/lock/StripedLock.java @@ -18,14 +18,10 @@ public class StripedLock extends ConcurrentAccessExperiment { int bucket = key % stripedLock.size(); Lock lock = stripedLock.get(bucket); try { - lock.tryLock(); + lock.lock(); map.put("key" + key, "value" + key); - } catch (Exception e) { - this.putSupplier(map, key); } finally { - try { - lock.unlock(); - } catch (Exception e) {} + lock.unlock(); } return null; }); @@ -36,14 +32,10 @@ public class StripedLock extends ConcurrentAccessExperiment { int bucket = key % stripedLock.size(); Lock lock = stripedLock.get(bucket); try { - lock.tryLock(); + lock.lock(); map.get("key" + key); - } catch (Exception e) { - this.getSupplier(map, key); } finally { - try { - lock.unlock(); - } catch (Exception e) {} + lock.unlock(); } return null; });