Java定制化线程池

一、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 <= 5; i++) {
            threadPoolExecutor.execute(() -> 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 <= 5; i++) {
            AsyncPrint4Runnable asyncPrint4Runnable = new AsyncPrint4Runnable();
            threadPoolExecutor.execute(asyncPrint4Runnable);
        }

        Thread.sleep(1000);

        System.out.println("结束");
    }

}
class AsyncPrint4Runnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "-jak");
    }
}

二、ThreadPoolExecutor.submit

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, ExecutionException {
        System.out.println("开始");

        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        for (int i = 0; i <= 5; i++) {
            AsyncPrint4Runnable asyncPrint4Runnable = new AsyncPrint4Runnable();
            Future<String> future = threadPoolExecutor.submit(asyncPrint4Runnable);
            String threadName = future.get();
            System.out.println("future: " + threadName);
        }

        Thread.sleep(1000);

        System.out.println("结束");
    }

}
class AsyncPrint4Runnable implements Callable<String> {
    @Override
    public String call() throws Exception {
        String threadName = Thread.currentThread().getName() + "-jak";
        System.out.println(threadName);
        return threadName;
    }
}
public class MyThreadPoolDemo {

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2,
                5,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        return threadPool;
    }

    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = getThreadPoolExecutor();

        try {
            //模拟10个用户来办理业务,每个用户就是一个来自外部的请求线程
            for (int i=1; i <=9; i++) {
                Future<String> future = threadPool.submit(() -> {
                    String str = "jak";
                    System.out.println(str);
                    return str;
                });
                String futureResult = future.get();
                System.out.println("futrure" + futureResult);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}

 Java线程池详解Java创建线程的方式

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值