工作线程数(线程池中线程数量) 设多少合适?

根据CPU的核数和计算能力设置
但是一个java程序启动本身就会启动很多线程,帮我们分配资源的线程,还有操作系统的线程等
出于安全的角度,还需要给CPU留有20%的余地
实际需要压测,得到一个合理的数据。

当然也可以根据公式说明:

Nthreads = NcPu * Ucpu *(1 + W/C)其中:
Ncpu是处理器的核的数目,可以通过Runtime.getRuntime().availableProcessors()得到
Ucpu是期望的CPU利用率( 该值应该介于0和1之间)

具体设置多少,也可以根据实际运行的情况。下面代码展示:

package com.zy.c_000_threadbasic;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

// 验证是否创建线程数越多越好
public class To1_MultiVSSingle_ContextSwitch {

    private static double[] nums = new double[100000000];
    private static Random random = new Random();
    private static DecimalFormat df = new DecimalFormat("0.00");

    static {
        for (int i = 0; i < nums.length; i++) {
            // 用于返回一个大于或等于 0.0 且小于 1.0 的随机浮点数
            nums[i] = random.nextDouble();
        }
    }


    public static void main(String[] args) throws Exception {
        m1();
        m2();
        m3();
    }


    // 单线程运行
    private static void m1() {
        long start = System.currentTimeMillis();
        double result = 0.0;
        for (int i = 0; i < nums.length; i++) {
            result += nums[i];
        }
        long end = System.currentTimeMillis();
        System.out.println("m1:" + (end - start) + "  result= " + df.format(result));
    }


    // 使用多线程的思维(2个)
    static double result1 = 0.0, result2 = 0.0, result = 0.0;

    private static void m2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < nums.length / 2; i++) {
                result1 += nums[i];
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = nums.length / 2; i < nums.length; i++) {
                result2 += nums[i];
            }
        });

        long start = System.currentTimeMillis();
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        result = result1 + result2;
        long end = System.currentTimeMillis();
        System.out.println("m2:" + (end - start) + "  result= " + df.format(result));
    }

    // 使用多线程的思维(多个)
    private static void m3() throws InterruptedException {
        final int threadCount = 32;
        Thread[] threads = new Thread[threadCount];
        double[] results = new double[threadCount];
        final int segmentCount = nums.length / threadCount;
        // 初始化,指定线程个数
        CountDownLatch latch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            int m = i;
            threads[i] = new Thread(() -> {
                for (int j = m * segmentCount; j < (m + 1) * segmentCount && j < nums.length; j++) {
                    results[m] += nums[j];
                }
                // 每个线程执行后执行latch.countDown();,代表一个线程执行完成,待完成的线程数减1。
                latch.countDown();
            });

        }
        double resultM3 = 0.0;
        long start = System.currentTimeMillis();
        for (Thread t : threads) {
            t.start();
        }
        // 阻塞该线程,等待其他子线程完成。
        latch.await();
        for (int i = 0; i < results.length; i++) {
            resultM3 += results[i];
        }
        long end = System.currentTimeMillis();
        System.out.println("m3:" + (end - start) + "  result= " + df.format(resultM3));
    }

}

猜你喜欢

转载自blog.csdn.net/m0_70299172/article/details/130775109
今日推荐