您当前的位置: 首页 >  Java

星夜孤帆

暂无认证

  • 8浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java定制化线程池

星夜孤帆 发布时间:2021-08-25 18:16:58 ,浏览量:8

一、ThreadPoolExecutor.execute

public class CustomThreadPool {

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        AtomicInteger threadNumber = new AtomicInteger();
        return new ThreadPoolExecutor(4,
                8,
                1,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue(1000),
                r -> {
                    Thread thread = new Thread(r,"jak-custom-thread-pool-" + threadNumber.getAndIncrement());
                    if (thread.isDaemon()) {
                        thread.setDaemon(true);
                    }
                    return thread;
                });
    }

    public static ThreadPoolExecutor getThreadPoolExecutorNoLambda() {
        AtomicInteger threadNumber = new AtomicInteger();
        return new ThreadPoolExecutor(4,
                8,
                1,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue(1000),
                new ThreadFactory() {
                    @Override
                    public Thread newThread(@NotNull Runnable r) {
                        Thread thread = new Thread(r, "jak-custom-thread-pool-" + threadNumber.getAndIncrement());
                        if (thread.isDaemon()) {
                            thread.setDaemon(false);
                        }
                        return null;
                    }
                });
    }


    private static void asyncPrint() {
        System.out.println(Thread.currentThread().getName() + "-jak");
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("开始");

        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        for (int i = 0; i  asyncPrint());
        }

        Thread.sleep(1000);

        System.out.println("结束");
    }
}
public class CustomThreadPool {

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        AtomicInteger threadNumber = new AtomicInteger();
        return new ThreadPoolExecutor(4,
                8,
                1,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue(1000),
                r -> {
                    Thread thread = new Thread(r,"jak-custom-thread-pool-" + threadNumber.getAndIncrement());
                    if (thread.isDaemon()) {
                        thread.setDaemon(true);
                    }
                    return thread;
                });
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("开始");

        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        for (int i = 0; i  {
                    Thread thread = new Thread(r,"jak-custom-thread-pool-" + threadNumber.getAndIncrement());
                    if (thread.isDaemon()) {
                        thread.setDaemon(true);
                    }
                    return thread;
                });
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        System.out.println("开始");

        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        for (int i = 0; i             
关注
打赏
1636984416
查看更多评论
0.1155s