BAEL-3855
This commit is contained in:
parent
880f922df3
commit
0e3a4221fe
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in New Issue