1、并行
并行代表充分利用多核 cpu 的優(yōu)勢(shì),提高運(yùn)行效率。
想象下面的場(chǎng)景,執(zhí)行 3 個(gè)計(jì)算,最后將計(jì)算結(jié)果匯總。
計(jì)算 1 花費(fèi) 10 ms
?
計(jì)算 2 花費(fèi) 11 ms
?
計(jì)算 3 花費(fèi) 9 ms
?
匯總需要 1 ms
-
如果是串行執(zhí)行,那么總共花費(fèi)的時(shí)間是
10 + 11 + 9 + 1 = 31ms
-
但如果是四核 cpu,各個(gè)核心分別使用線(xiàn)程 1 執(zhí)行計(jì)算 1,線(xiàn)程 2 執(zhí)行計(jì)算 2,線(xiàn)程 3 執(zhí)行計(jì)算 3,那么 3 個(gè)線(xiàn)程是并行的,花費(fèi)時(shí)間只取決于最長(zhǎng)的那個(gè)線(xiàn)程運(yùn)行的時(shí)間,即
11ms
最后加上匯總時(shí)間只會(huì)花費(fèi)12ms
注意
需要在多核 cpu 才能提高效率,單核仍然時(shí)是輪流執(zhí)行
2、設(shè)計(jì)
1) 環(huán)境搭建
-
基準(zhǔn)測(cè)試工具選擇,使用了比較靠譜的基準(zhǔn)測(cè)試框架 JMH,它會(huì)執(zhí)行程序預(yù)熱(會(huì)對(duì)反復(fù)執(zhí)行的代碼進(jìn)行優(yōu)化),執(zhí)行多次測(cè)試并平均
-
cpu 核數(shù)限制,有兩種思路
-
建議使用虛擬機(jī),分配合適的核
-
使用 msconfig,分配合適的核,需要重啟比較麻煩
-
-
并行計(jì)算方式的選擇
-
最初想直接使用 parallel stream,后來(lái)發(fā)現(xiàn)它有自己的問(wèn)題
-
改為了自己手動(dòng)控制 thread,實(shí)現(xiàn)簡(jiǎn)單的并行計(jì)算
-
-
引入jar包
? ?<dependencies>
? ? ? ?<dependency>
? ? ? ? ? ?<groupId>org.openjdk.jmh</groupId>
? ? ? ? ? ?<artifactId>jmh-core</artifactId>
? ? ? ? ? ?<version>1.0</version>
? ? ? ?</dependency>
? ? ? ?<dependency>
? ? ? ? ? ?<groupId>org.openjdk.jmh</groupId>
? ? ? ? ? ?<artifactId>jmh-generator-annprocess</artifactId>
? ? ? ? ? ?<version>1.0</version>
? ? ? ? ? ?<scope>provided</scope>
? ? ? ?</dependency>
? ?</dependencies>
如下代碼測(cè)試
?
package org.sample;
?
import java.util.Arrays;
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;
?
@Fork(1)
@BenchmarkMode(Mode.AverageTime) ?//測(cè)試模式 這里平均時(shí)間
@Warmup(iterations=3) // 熱身次數(shù)
@Measurement(iterations=5) // 五輪驗(yàn)證
public class MyBenchmark {
static int[] ARRAY = new int[1000_000_00]; // 這里可根據(jù)電腦配置調(diào)整大小,避免堆溢出錯(cuò)誤
static {
Arrays.fill(ARRAY, 1);
}
? ?// 建立4個(gè)線(xiàn)程
? ?@Benchmark
? ?public int c() throws Exception {
? ? ? ?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();
? }
? ?// 單線(xiàn)程
? ?@Benchmark
? ?public int d() throws Exception {
? ? ? ?int[] array = ARRAY;
? ? ? ?FutureTask<Integer> t1 = new FutureTask<>(()->{
? ? ? int sum = 0;
? ? ? for(int i = 0; i < 1000_000_00;i++) {
? ? ? sum += array[0+i];
? ? ? }
? ? ? return sum;
? ? ? });
? ? ? ?new Thread(t1).start();
? ? ? ?return t1.get();
? }
}
2) 雙核 CPU(4個(gè)邏輯CPU)
C:\Users\lenovo\eclipse-workspace\test>java -jar target/benchmarks.jar
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# 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: org.sample.MyBenchmark.c
?
# Run progress: 0.00% complete, ETA 00:00:16
# Fork: 1 of 1
# Warmup Iteration ? 1: 0.022 s/op
# Warmup Iteration ? 2: 0.019 s/op
# Warmup Iteration ? 3: 0.020 s/op
Iteration ? 1: 0.020 s/op
Iteration ? 2: 0.020 s/op
Iteration ? 3: 0.020 s/op
Iteration ? 4: 0.020 s/op
Iteration ? 5: 0.020 s/op
?
?
Result: 0.020 ±(99.9%) 0.001 s/op [Average]
Statistics: (min, avg, max) = (0.020, 0.020, 0.020), stdev = 0.000
Confidence interval (99.9%): [0.019, 0.021]
?
?
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# 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: org.sample.MyBenchmark.d
?
# Run progress: 50.00% complete, ETA 00:00:10
# Fork: 1 of 1
# Warmup Iteration ? 1: 0.042 s/op
# Warmup Iteration ? 2: 0.042 s/op
# Warmup Iteration ? 3: 0.041 s/op
Iteration ? 1: 0.043 s/op
Iteration ? 2: 0.042 s/op
Iteration ? 3: 0.042 s/op
Iteration ? 4: 0.044 s/op
Iteration ? 5: 0.042 s/op
?
?
Result: 0.043 ±(99.9%) 0.003 s/op [Average]
Statistics: (min, avg, max) = (0.042, 0.043, 0.044), stdev = 0.001
Confidence interval (99.9%): [0.040, 0.045]
?
?
# Run complete. Total time: 00:00:20
?
Benchmark ? ? ? ? ? Mode Samples Score Score error Units
o.s.MyBenchmark.c ? avgt ? ? ? ?5 ?0.020 ? ? ? ?0.001 ? s/op
o.s.MyBenchmark.d ? avgt ? ? ? ?5 ?0.043 ? ? ? ?0.003 ? s/op
在最后兩行的結(jié)論中,可以看到多核下,效率提升還是很明顯的,快了一倍左右
3) 單核 CPU
單核cpu建議開(kāi)虛擬機(jī)測(cè)試,這里就不驗(yàn)證了。直接看結(jié)果
C:\Users\lenovo\eclipse-workspace\test>java -jar target/benchmarks.jar
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# 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: org.sample.MyBenchmark.c
?
# Run progress: 0.00% complete, ETA 00:00:16
# Fork: 1 of 1
# Warmup Iteration ? 1: 0.064 s/op
# Warmup Iteration ? 2: 0.052 s/op
# Warmup Iteration ? 3: 1.127 s/op
Iteration ? 1: 0.053 s/op
Iteration ? 2: 0.052 s/op
Iteration ? 3: 0.053 s/op
Iteration ? 4: 0.057 s/op
Iteration ? 5: 0.088 s/op
?
?
Result: 0.061 ±(99.9%) 0.060 s/op [Average]
Statistics: (min, avg, max) = (0.052, 0.061, 0.088), stdev = 0.016
Confidence interval (99.9%): [0.001, 0.121]
?
?
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# 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: org.sample.MyBenchmark.d
?
# Run progress: 50.00% complete, ETA 00:00:11
# Fork: 1 of 1
# Warmup Iteration ? 1: 0.054 s/op
# Warmup Iteration ? 2: 0.053 s/op
# Warmup Iteration ? 3: 0.051 s/op
Iteration ? 1: 0.096 s/op
Iteration ? 2: 0.054 s/op
Iteration ? 3: 0.065 s/op
Iteration ? 4: 0.050 s/op
Iteration ? 5: 0.055 s/op
?
?
Result: 0.064 ±(99.9%) 0.071 s/op [Average]
Statistics: (min, avg, max) = (0.050, 0.064, 0.096), stdev = 0.018
Confidence interval (99.9%): [-0.007, 0.135]
?
?
# Run complete. Total time: 00:00:22
?
Benchmark ? ? ? ? ? Mode Samples Score Score error Units
o.s.MyBenchmark.c ? avgt ? ? ? ?5 ?0.061 ? ? ? ?0.060 ? s/op
o.s.MyBenchmark.d ? avgt ? ? ? ?5 ?0.064 ? ? ? ?0.071 ? s/op
可以看到性能幾乎是一樣的
4) 結(jié)論
-
單核 cpu 下,多線(xiàn)程不能實(shí)際提高程序運(yùn)行效率,只是為了能夠在不同的任務(wù)之間切換,不同線(xiàn)程輪流使用 cpu ,不至于一個(gè)線(xiàn)程總占用 cpu,別的線(xiàn)程沒(méi)法干活
-
多核 cpu 可以并行跑多個(gè)線(xiàn)程,但能否提高程序運(yùn)行效率還是要分情況的
-
有些任務(wù),經(jīng)過(guò)精心設(shè)計(jì),將任務(wù)拆分,并行執(zhí)行,當(dāng)然可以提高程序的運(yùn)行效率。但不是所有計(jì)算任務(wù)都能拆分
-
也不是所有任務(wù)都需要拆分,任務(wù)的目的如果不同,談拆分和效率沒(méi)啥意義
-
-
IO 操作不占用 cpu,只是我們一般拷貝文件使用的是【阻塞 IO】,這時(shí)相當(dāng)于線(xiàn)程雖然不用 cpu,但需要一直等待 IO 結(jié)束,沒(méi)能充分利用線(xiàn)程。所以才有【非阻塞 IO】和【異步 IO】來(lái)提升線(xiàn)程的利用率文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-412561.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-412561.html
到了這里,關(guān)于Java并發(fā)(二)----初次使用多線(xiàn)程并行提高效率的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!