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.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.Map;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
@ -25,28 +26,28 @@ public class ConcurrentAccessBenchmark {
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void singleLockHashMap() throws InterruptedException {
(new SingleLock()).doWork(new HashMap<String,String>(), THREADS, SLOTS);
public Map<String,String> singleLockHashMap() throws InterruptedException {
return (new SingleLock()).doWork(new HashMap<String,String>(), THREADS, SLOTS);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void stripedLockHashMap() throws InterruptedException {
(new StripedLock(BUCKETS)).doWork(new HashMap<String,String>(), THREADS, SLOTS);
public Map<String,String> stripedLockHashMap() throws InterruptedException {
return (new StripedLock(BUCKETS)).doWork(new HashMap<String,String>(), THREADS, SLOTS);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void singleLockConcurrentHashMap() throws InterruptedException {
(new SingleLock()).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
public Map<String,String> singleLockConcurrentHashMap() throws InterruptedException {
return (new SingleLock()).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void stripedLockConcurrentHashMap() throws InterruptedException {
(new StripedLock(BUCKETS)).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
public Map<String,String> stripedLockConcurrentHashMap() throws InterruptedException {
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) {
return (()-> {
int bucket = key % stripedLock.size();
int bucket = key % stripedLock.size();
Lock lock = stripedLock.get(bucket);
try {
lock.tryLock();
@ -33,7 +33,7 @@ public class StripedLock extends ConcurrentAccessExperiment {
protected Supplier<?> getSupplier(Map<String,String> map, int key) {
return (()-> {
int bucket = key % stripedLock.size();
int bucket = key % stripedLock.size();
Lock lock = stripedLock.get(bucket);
try {
lock.tryLock();