Skip to content

Commit e039960

Browse files
tarangbhalodiapivovarit
authored andcommitted
BAEL-1422: Guide to ThreadLocalRandom [tarangbhalodia@gmail.com] (eugenp#3347)
* BAEL-1422: measure performance of Random and ThreadLocalRandom using JMH * BAEL-1422: updated benchmarking examples of Random and ThreadLocalRandom to use newWorkStealingPool that leverages ForkJoinPool * BAEL-1422: refactored benchmarking examples for comparing performance of ThreadLocalRandom and Random - initialised the collection of Callable before running benchmarking - removed for loop for submitting task and instead used executor.invokeAll(collection_of_callable)
1 parent e99619d commit e039960

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.threadlocalrandom;
2+
3+
import org.openjdk.jmh.runner.Runner;
4+
import org.openjdk.jmh.runner.options.Options;
5+
import org.openjdk.jmh.runner.options.OptionsBuilder;
6+
7+
public class ThreadLocalRandomBenchMarkRunner {
8+
9+
public static void main(String[] args) throws Exception {
10+
11+
Options options = new OptionsBuilder().include(ThreadLocalRandomBenchMarker.class.getSimpleName())
12+
.threads(1)
13+
.forks(1)
14+
.shouldFailOnError(true)
15+
.shouldDoGC(true)
16+
.jvmArgs("-server")
17+
.build();
18+
19+
new Runner(options).run();
20+
21+
}
22+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.threadlocalrandom;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
import java.util.concurrent.Callable;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.ThreadLocalRandom;
10+
import java.util.concurrent.TimeUnit;
11+
12+
import org.openjdk.jmh.annotations.Benchmark;
13+
import org.openjdk.jmh.annotations.BenchmarkMode;
14+
import org.openjdk.jmh.annotations.Level;
15+
import org.openjdk.jmh.annotations.Mode;
16+
import org.openjdk.jmh.annotations.OutputTimeUnit;
17+
import org.openjdk.jmh.annotations.Scope;
18+
import org.openjdk.jmh.annotations.Setup;
19+
import org.openjdk.jmh.annotations.State;
20+
import org.openjdk.jmh.annotations.Warmup;
21+
22+
@BenchmarkMode(Mode.AverageTime)
23+
@Warmup(iterations = 1)
24+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
25+
@State(Scope.Benchmark)
26+
public class ThreadLocalRandomBenchMarker {
27+
28+
List<Callable<Integer>> randomCallables = new ArrayList<>();
29+
List<Callable<Integer>> threadLocalRandomCallables = new ArrayList<>();
30+
31+
@Setup(Level.Iteration)
32+
public void init() {
33+
Random random = new Random();
34+
randomCallables = new ArrayList<>();
35+
threadLocalRandomCallables = new ArrayList<>();
36+
for (int i = 0; i < 1000; i++) {
37+
randomCallables.add(() -> {
38+
return random.nextInt();
39+
});
40+
}
41+
42+
for (int i = 0; i < 1000; i++) {
43+
threadLocalRandomCallables.add(() -> {
44+
return ThreadLocalRandom.current()
45+
.nextInt();
46+
});
47+
}
48+
}
49+
50+
@Benchmark
51+
public void randomValuesUsingRandom() throws InterruptedException {
52+
ExecutorService executor = Executors.newWorkStealingPool();
53+
executor.invokeAll(randomCallables);
54+
executor.shutdown();
55+
}
56+
57+
@Benchmark
58+
public void randomValuesUsingThreadLocalRandom() throws InterruptedException {
59+
ExecutorService executor = Executors.newWorkStealingPool();
60+
executor.invokeAll(threadLocalRandomCallables);
61+
executor.shutdown();
62+
}
63+
64+
}

0 commit comments

Comments
 (0)