当前位置:网站首页>多线程的应用 - 提升效率
多线程的应用 - 提升效率
2022-06-24 09:47:00 【兀坐晴窗独饮茶】
环境搭建
基本介绍
基准测试工具选择,使用了比较靠谱的 JMH,它会执行程序预热,执行多次测试并平均
cpu 核数限制,有两种思路
- 使用虚拟机,分配合适的核
- 使用 msconfig,分配合适的核,需要重启比较麻烦
并行计算方式的选择
创建项目
点击 Add , 添加 groupId : org.openjdk.jmh , 然后添加 ArtifactId : jmh-java-benchmark-archetype


也可以通过maven命令创建
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.openjdk.jmh -
DarchetypeArtifactId=jmh-java-benchmark-archetype
-DgroupId=cn.knightzz
-DartifactId=apply-efficiency
-Dversion=1.0
创建完成以后记得去pom文件中将java编译版本<javac.target>1.8</javac.target>从1.6修为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>
编写测试代码
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 王天赐 * @title: MyBenchmark * @projectName hm-juc-codes * @description: 测试多线程提高执行效率 * @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) // 统计程序平均耗时
@Warmup(iterations=3) // 程序热身次数
@Measurement(iterations=5) // 测试次数
@SuppressWarnings("all")
public class MyBenchmark {
static int[] ARRAY = new int[1000_000_00];
static {
// 将数组里面填充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;
}
}
使用 maven package 打成 jar包

然后命令台进入target目录下 , 执行 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
执行结果如上面所示 : 可以看到效率差了很多, 多线程的执行 耗费 0.022 s , 而单线程耗费 0.039 s (单位是秒)
边栏推荐
- 抓包工具charles实践分享
- 【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)
- 学习使用phpstripslashe函数去除反斜杠
- 【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
- uniapp实现点击拨打电话功能
- Record the range of data that MySQL update will lock
- 2022年能源与环境工程国际研讨会(CoEEE 2022)
- 5. dish management business development
- 2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
- Leetcode-498: diagonal traversal
猜你喜欢
随机推荐
Role of message queuing
Leetcode-223: rectangular area
形状变化loader加载jsjs特效代码
leetCode-223: 矩形面积
如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
Flink checkPoint和SavePoint
微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
跨域概述,简单积累
tf. errors
【JS逆向分享】某个网站社区信息
静态链接库和动态链接库的区别
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
SQL Server AVG函数取整问题
6.套餐管理业务开发
【IEEE出版】2022年服务机器人国际研讨会(IWoSR 2022)
leetCode-面试题 16.06: 最小差
[EI分享] 2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance








