BAEL-3855
This commit is contained in:
parent
2302a4abb7
commit
407861afac
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.concurrent.lock;
|
package com.baeldung.concurrent.lock;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.openjdk.jmh.annotations.Benchmark;
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
@ -7,14 +9,12 @@ import org.openjdk.jmh.annotations.BenchmarkMode;
|
|||||||
import org.openjdk.jmh.annotations.Fork;
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
import org.openjdk.jmh.annotations.Mode;
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
import org.openjdk.jmh.annotations.Param;
|
|
||||||
import org.openjdk.jmh.annotations.Scope;
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
import org.openjdk.jmh.annotations.Setup;
|
|
||||||
import org.openjdk.jmh.annotations.State;
|
import org.openjdk.jmh.annotations.State;
|
||||||
import org.openjdk.jmh.annotations.Warmup;
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
|
|
||||||
@State(Scope.Thread)
|
@State(Scope.Thread)
|
||||||
@Fork(value = 1)
|
@Fork(value = 2)
|
||||||
@Warmup(iterations = 0)
|
@Warmup(iterations = 0)
|
||||||
public class ConcurrentAccessBenchmark {
|
public class ConcurrentAccessBenchmark {
|
||||||
ConcurrentAccessExperiment accessMyMap;
|
ConcurrentAccessExperiment accessMyMap;
|
||||||
@ -22,28 +22,31 @@ public class ConcurrentAccessBenchmark {
|
|||||||
static final int THREADS = 10000;
|
static final int THREADS = 10000;
|
||||||
static final int BUCKETS = Runtime.getRuntime().availableProcessors() * SLOTS;
|
static final int BUCKETS = Runtime.getRuntime().availableProcessors() * SLOTS;
|
||||||
|
|
||||||
@Param({"Single Lock", "Striped Lock"})
|
@Benchmark
|
||||||
private String lockType;
|
@BenchmarkMode(Mode.Throughput)
|
||||||
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||||
@Param({"HashMap", "ConcurrentHashMap"})
|
public void singleLockHashMap() throws InterruptedException {
|
||||||
private String mapType;
|
(new SingleLock()).doWork(new HashMap<String,String>(), THREADS, SLOTS);
|
||||||
|
|
||||||
@Setup
|
|
||||||
public void setup() {
|
|
||||||
switch (lockType) {
|
|
||||||
case "Single Lock":
|
|
||||||
accessMyMap = new SingleLock();
|
|
||||||
break;
|
|
||||||
case "Striped Lock":
|
|
||||||
accessMyMap = new StripedLock(BUCKETS);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@BenchmarkMode(Mode.Throughput)
|
@BenchmarkMode(Mode.Throughput)
|
||||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||||
public void test() throws InterruptedException {
|
public void stripedLockHashMap() throws InterruptedException {
|
||||||
accessMyMap.doWork(mapType, THREADS, SLOTS);
|
(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.Throughput)
|
||||||
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||||
|
public void stripedLockConcurrentHashMap() throws InterruptedException {
|
||||||
|
(new StripedLock(BUCKETS)).doWork(new ConcurrentHashMap<String,String>(), THREADS, SLOTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,14 @@
|
|||||||
package com.baeldung.concurrent.lock;
|
package com.baeldung.concurrent.lock;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
|
|
||||||
public abstract class ConcurrentAccessExperiment {
|
public abstract class ConcurrentAccessExperiment {
|
||||||
|
|
||||||
public ConcurrentAccessExperiment() {
|
public final Map<String,String> doWork(Map<String,String> map, int threads, int slots) {
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String,String> doMapSetup(String typeOfMap) {
|
|
||||||
switch (typeOfMap) {
|
|
||||||
case "HashMap":
|
|
||||||
return new HashMap<String,String>();
|
|
||||||
case "ConcurrentHashMap":
|
|
||||||
return new ConcurrentHashMap<String,String>();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void doWork(String typeOfMap, int threads, int slots) {
|
|
||||||
CompletableFuture<?>[] requests = new CompletableFuture<?>[threads * slots];
|
CompletableFuture<?>[] requests = new CompletableFuture<?>[threads * slots];
|
||||||
Map<String,String> map = doMapSetup(typeOfMap);
|
|
||||||
|
|
||||||
for (int i = 0; i < threads; i++) {
|
for (int i = 0; i < threads; i++) {
|
||||||
requests[slots * i + 0] = CompletableFuture.supplyAsync(putSupplier(map, i));
|
requests[slots * i + 0] = CompletableFuture.supplyAsync(putSupplier(map, i));
|
||||||
@ -33,6 +17,8 @@ public abstract class ConcurrentAccessExperiment {
|
|||||||
requests[slots * i + 3] = CompletableFuture.supplyAsync(getSupplier(map, i));
|
requests[slots * i + 3] = CompletableFuture.supplyAsync(getSupplier(map, i));
|
||||||
}
|
}
|
||||||
CompletableFuture.allOf(requests).join();
|
CompletableFuture.allOf(requests).join();
|
||||||
|
|
||||||
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Supplier<?> putSupplier(Map<String,String> map, int key);
|
protected abstract Supplier<?> putSupplier(Map<String,String> map, int key);
|
||||||
|
@ -10,12 +10,7 @@ public class StripedLock extends ConcurrentAccessExperiment {
|
|||||||
Striped<Lock> lock;
|
Striped<Lock> lock;
|
||||||
|
|
||||||
public StripedLock(int buckets) {
|
public StripedLock(int buckets) {
|
||||||
lock = getStripedLock(buckets);
|
lock = Striped.lock(buckets);
|
||||||
}
|
|
||||||
|
|
||||||
private Striped<Lock> getStripedLock(int buckets) {
|
|
||||||
Striped<Lock> map = Striped.lock(buckets);
|
|
||||||
return map;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected synchronized Supplier<?> putSupplier(Map<String,String> map, int key) {
|
protected synchronized Supplier<?> putSupplier(Map<String,String> map, int key) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user