当前位置:网站首页>Multithreaded applications - improve efficiency
Multithreaded applications - improve efficiency
2022-06-24 10:32:00 【Sit at a sunny window and drink tea alone】
Environment building
Basic introduction
Benchmarking tool selection , Used a more reliable JMH, It will execute the program , Perform multiple tests and average
cpu Audit limit , There are two ways of thinking
- Using virtual machines , Assign the appropriate core
- Use msconfig, Assign the appropriate core , It's troublesome to restart
Choice of parallel computing methods
Create project
Click on Add , add to groupId : org.openjdk.jmh , Then add ArtifactId : jmh-java-benchmark-archetype


It can also be done through maven Command to create
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.openjdk.jmh -
DarchetypeArtifactId=jmh-java-benchmark-archetype
-DgroupId=cn.knightzz
-DartifactId=apply-efficiency
-Dversion=1.0
Remember to go after the creation pom file java Compiled version <javac.target>1.8</javac.target> from 1.6 Cultivation 1.8
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jmh.version>1.0</jmh.version>
<javac.target>1.8</javac.target>
<uberjar.name>benchmarks</uberjar.name>
</properties>
Write test code
package cn.knightzz.benchmark;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Warmup;
/** * @author Wang Tianci * @title: MyBenchmark * @projectName hm-juc-codes * @description: Test multithreading to improve execution efficiency * @website <a href="http://knightzz.cn/">http://knightzz.cn/</a> * @github <a href="https://github.com/knightzz1998">https://github.com/knightzz1998</a> * @create: 2022-06-19 15:59 */
@Fork(1)
@BenchmarkMode(Mode.AverageTime) // Average time taken for statistical procedures
@Warmup(iterations=3) // Program warm-up times
@Measurement(iterations=5) // Number of tests
@SuppressWarnings("all")
public class MyBenchmark {
static int[] ARRAY = new int[1000_000_00];
static {
// Fill the array with 1
Arrays.fill(ARRAY, 1);
}
@Benchmark
public int c() throws ExecutionException, InterruptedException {
int[] array = ARRAY;
FutureTask<Integer> t1 = new FutureTask<>(() -> {
int sum = 0;
for (int i = 0; i < 250_000_00; i++) {
sum += array[0+i];
}
return sum;
});
FutureTask<Integer> t2 = new FutureTask<>(() -> {
int sum = 0;
for (int i = 0; i < 250_000_00; i++) {
sum += array[250_000_00 + i];
}
return sum;
});
FutureTask<Integer> t3 = new FutureTask<>(() -> {
int sum = 0;
for (int i = 0; i < 250_000_00; i++) {
sum += array[500_000_00 + i];
}
return sum;
});
FutureTask<Integer> t4 = new FutureTask<>(() -> {
int sum = 0;
for (int i = 0; i < 250_000_00; i++) {
sum += array[750_000_00 + i];
}
return sum;
});
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
new Thread(t4).start();
return t1.get() + t2.get() + t3.get() + t4.get();
}
@Benchmark
public int d() {
int[] array = ARRAY;
int sum = 0;
for (int i = 0; i < 1000_000_00; i++) {
sum += array[0 + i];
}
return sum;
}
}
Use maven package become involved jar package

Then the command desk enters target Under the table of contents , perform java -jar -Xmx2G benchmarks.jar
$ java -jar -Xmx2G benchmarks.jar
# VM invoker: C:\dev\Java\jre\bin\java.exe
# VM options: -Xmx2G
# Warmup: 3 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: cn.knightzz.benchmark.MyBenchmark.c
# Run progress: 0.00% complete, ETA 00:00:16
# Fork: 1 of 1
# Warmup Iteration 1: 0.019 s/op
# Warmup Iteration 2: 0.020 s/op
# Warmup Iteration 3: 0.019 s/op
Iteration 1: 0.023 s/op
Iteration 2: 0.020 s/op
Iteration 3: 0.020 s/op
Iteration 4: 0.026 s/op
Iteration 5: 0.022 s/op
Result: 0.022 ±(99.9%) 0.009 s/op [Average]
Statistics: (min, avg, max) = (0.020, 0.022, 0.026), stdev = 0.002
Confidence interval (99.9%): [0.013, 0.031]
# VM invoker: C:\dev\Java\jre\bin\java.exe
# VM options: -Xmx2G
# Warmup: 3 iterations, 1 s each
Iteration 4: 0.038 s/op
Iteration 5: 0.039 s/op
Result: 0.039 ±(99.9%) 0.002 s/op [Average]
Statistics: (min, avg, max) = (0.038, 0.039, 0.039), stdev = 0.000
Confidence interval (99.9%): [0.037, 0.041]
# Run complete. Total time: 00:00:20
Benchmark Mode Samples Score Score error Units
c.k.b.MyBenchmark.c avgt 5 0.022 0.009 s/op
c.k.b.MyBenchmark.d avgt 5 0.039 0.002 s/op
The execution result is as shown above : You can see that the efficiency is much lower , Multithreading execution cost 0.022 s , Single thread cost 0.039 s ( The unit is seconds )
边栏推荐
- Image click enlargement and adaptive size in the applet rich text
- Appium automation test foundation - mobile end test environment construction (I)
- 自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏
- 4.分类管理业务开发
- Cross domain overview, simple accumulation
- leetCode-929: 独特的电子邮件地址
- Wechat applet rich text picture width height adaptive method introduction (rich text)
- 线程运行原理
- Leetcode-1089: replication zero
- 机械臂速成小指南(三):机械臂的机械结构
猜你喜欢
随机推荐
解决DBeaver SQL Client 连接phoenix查询超时
Safety and food security for teachers and students of the trapped Yingxi middle school
2. login and exit function development
np. float32()
线程的 sleep() 方法与 wait() 方法的区别
希尔排序图文详解+代码实现
Six states of threads
Leetcode - 498: traversée diagonale
Learn to use the phpstripslush function to remove backslashes
uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
JMeter接口测试工具基础 — Badboy工具
Thread pool execution process
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
Resolved: methods with the same name as their class will not be constructors in
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
5.菜品管理业务开发
Machine learning - principal component analysis (PCA)
【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)
3.员工的增删改查
Solve the timeout of Phoenix query of dbeaver SQL client connection









