Java併發(二)----初次使用多線程並行提高效率

1、並行

並行代表充分利用多核 cpu 的優勢,提高運行效率。

想象下面的場景,執行 3 個計算,最後將計算結果彙總。

計算 1 花費 10 ms
​
計算 2 花費 11 ms
​
計算 3 花費 9 ms
​
彙總需要 1 ms
  • 如果是串行執行,那麼總共花費的時間是 10 + 11 + 9 + 1 = 31ms

  • 但如果是四核 cpu,各個核心分別使用線程 1 執行計算 1,線程 2 執行計算 2,線程 3 執行計算 3,那麼 3 個線程是並行的,花費時間只取決於最長的那個線程運行的時間,即 11ms 最後加上彙總時間只會花費 12ms

注意

需要在多核 cpu 才能提高效率,單核仍然時是輪流執行

2、設計

1) 環境搭建

  • 基準測試工具選擇,使用了比較靠譜的基準測試框架 JMH,它會執行程序預熱(會對反覆執行的代碼進行優化),執行多次測試並平均

  • cpu 核數限制,有兩種思路

    1. 建議使用虛擬機,分配合適的核

    2. 使用 msconfig,分配合適的核,需要重啓比較麻煩

  • 並行計算方式的選擇

    1. 最初想直接使用 parallel stream,後來發現它有自己的問題

    2. 改爲了自己手動控制 thread,實現簡單的並行計算

  • 引入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>

如下代碼測試

​
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)  //測試模式 這裏平均時間
@Warmup(iterations=3) // 熱身次數
@Measurement(iterations=5) // 五輪驗證
public class MyBenchmark {
    static int[] ARRAY = new int[1000_000_00]; // 這裏可根據電腦配置調整大小,避免堆溢出錯誤
    static {
        Arrays.fill(ARRAY, 1);
    }
    // 建立4個線程
    @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();
    }
    // 單線程
    @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個邏輯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

在最後兩行的結論中,可以看到多核下,效率提升還是很明顯的,快了一倍左右

3) 單核 CPU

單核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.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) 結論

  1. 單核 cpu 下,多線程不能實際提高程序運行效率,只是爲了能夠在不同的任務之間切換,不同線程輪流使用 cpu ,不至於一個線程總佔用 cpu,別的線程沒法幹活

  2. 多核 cpu 可以並行跑多個線程,但能否提高程序運行效率還是要分情況的

    • 有些任務,經過精心設計,將任務拆分,並行執行,當然可以提高程序的運行效率。但不是所有計算任務都能拆分

    • 也不是所有任務都需要拆分,任務的目的如果不同,談拆分和效率沒啥意義

  3. IO 操作不佔用 cpu,只是我們一般拷貝文件使用的是【阻塞 IO】,這時相當於線程雖然不用 cpu,但需要一直等待 IO 結束,沒能充分利用線程。所以纔有【非阻塞 IO】和【異步 IO】來提升線程的利用率

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章