hjg
2024-02-05 301115d5e96b56cd093eee3fcff2d60a15184162
JavaSource/com/mandi/fendan/util/ThreadPoolUtil.java
@@ -1,7 +1,7 @@
package com.mandi.fendan.util;
import javax.validation.constraints.NotNull;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -27,25 +27,27 @@
    //存活时间
    private long keepAliveTime = 1;
    private TimeUnit unit = TimeUnit.HOURS;
    private ThreadPoolExecutor executor;
//    private ThreadPoolExecutor executor;
    ScheduledThreadPoolExecutor executor;
    private ThreadPoolUtil() {
        //给corePoolSize赋值:当前设备可用处理器核心数*2 + 1,能够让cpu的效率得到最大程度执行(有研究论证的)
        corePoolSize = Runtime.getRuntime().availableProcessors() * 2 + 1;
        maxPoolSize = corePoolSize;
        executor = new ThreadPoolExecutor(
                //当某个核心任务执行完毕,会依次从缓冲队列中取出等待任务
                corePoolSize,
                // 然后new LinkedBlockingQueue(),然后maximumPoolSize,但是它的数量是包含了corePoolSize的
                maxPoolSize,
                //表示的是maximumPoolSize当中等待任务的存活时间
                keepAliveTime,
                unit,
                //缓冲队列,用于存放等待任务,Linked的先进先出
                new LinkedBlockingQueue(),
                new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
                new ThreadPoolExecutor.AbortPolicy()
        );
//        executor = new ThreadPoolExecutor(
//                //当某个核心任务执行完毕,会依次从缓冲队列中取出等待任务
//                corePoolSize,
//                // 然后new LinkedBlockingQueue(),然后maximumPoolSize,但是它的数量是包含了corePoolSize的
//                maxPoolSize,
//                //表示的是maximumPoolSize当中等待任务的存活时间
//                keepAliveTime,
//                unit,
//                //缓冲队列,用于存放等待任务,Linked的先进先出
//                new LinkedBlockingQueue(),
//                new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
//                new ThreadPoolExecutor.AbortPolicy()
//        );
          executor =  new ScheduledThreadPoolExecutor(corePoolSize);
    }
    public static ThreadPoolUtil getInstance() {
@@ -65,18 +67,36 @@
     * @param runnable
     */
    public void execute(Runnable runnable) {
        checkNull();
        if (runnable != null) {
            executor.execute(runnable);
        }
    }
    void checkNull() {
        if (executor == null) {
            executor = new ThreadPoolExecutor(
                    corePoolSize,
                    maxPoolSize,
                    keepAliveTime,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue(),
//            executor = new ThreadPoolExecutor(
//                    corePoolSize,
//                    maxPoolSize,
//                    keepAliveTime,
//                    TimeUnit.SECONDS,
//                    new LinkedBlockingQueue(),
//                    new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
//                    new ThreadPoolExecutor.AbortPolicy());
            executor =  new ScheduledThreadPoolExecutor(corePoolSize,
                    new DefaultThreadFactory(Thread.NORM_PRIORITY, "thread-pool-"),
                    new ThreadPoolExecutor.AbortPolicy());
        }
    }
    /**
     *
     * @param runnable
     * @param delay 延迟执行,单位毫秒
     */
    public void execute(Runnable runnable,long delay) {
        checkNull();
        if (runnable != null) {
            executor.execute(runnable);
            executor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
        }
    }