BAEL-3855

This commit is contained in:
Unknown 2020-03-13 16:33:34 +01:00
parent 880f922df3
commit 0e3a4221fe
2 changed files with 13 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package com.baeldung.concurrent.lock;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.Map;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.BenchmarkMode;
@ -25,28 +26,28 @@ public class ConcurrentAccessBenchmark {
@Benchmark @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public void singleLockHashMap() throws InterruptedException { public Map<String,String> singleLockHashMap() throws InterruptedException {
(new SingleLock()).doWork(new HashMap<String,String>(), THREADS, SLOTS); return (new SingleLock()).doWork(new HashMap<String,String>(), THREADS, SLOTS);
} }
@Benchmark @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public void stripedLockHashMap() throws InterruptedException { public Map<String,String> stripedLockHashMap() throws InterruptedException {
(new StripedLock(BUCKETS)).doWork(new HashMap<String,String>(), THREADS, SLOTS); return (new StripedLock(BUCKETS)).doWork(new HashMap<String,String>(), THREADS, SLOTS);
} }
@Benchmark @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public void singleLockConcurrentHashMap() throws InterruptedException { public Map<String,String> singleLockConcurrentHashMap() throws InterruptedException {
(new SingleLock()).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS); return (new SingleLock()).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
} }
@Benchmark @Benchmark
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
public void stripedLockConcurrentHashMap() throws InterruptedException { public Map<String,String> stripedLockConcurrentHashMap() throws InterruptedException {
(new StripedLock(BUCKETS)).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS); return (new StripedLock(BUCKETS)).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
} }
} }

View File

@ -15,7 +15,7 @@ public class StripedLock extends ConcurrentAccessExperiment {
protected Supplier<?> putSupplier(Map<String,String> map, int key) { protected Supplier<?> putSupplier(Map<String,String> map, int key) {
return (()-> { return (()-> {
int bucket = key % stripedLock.size(); int bucket = key % stripedLock.size();
Lock lock = stripedLock.get(bucket); Lock lock = stripedLock.get(bucket);
try { try {
lock.tryLock(); lock.tryLock();
@ -33,7 +33,7 @@ public class StripedLock extends ConcurrentAccessExperiment {
protected Supplier<?> getSupplier(Map<String,String> map, int key) { protected Supplier<?> getSupplier(Map<String,String> map, int key) {
return (()-> { return (()-> {
int bucket = key % stripedLock.size(); int bucket = key % stripedLock.size();
Lock lock = stripedLock.get(bucket); Lock lock = stripedLock.get(bucket);
try { try {
lock.tryLock(); lock.tryLock();