基于Executors工具类创建线程池

优先掌握ThreadPoolExecutor线程类的构造方法

ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler);
corePoolSize核心线程数
maximumPoolSize最大线程数
keepAliveTime应急线程回收空闲时间
unit应急线程回收空闲时间单位
workQueue队列
threadFactory
RejectedExecutionHandler handler

创建

一、创建一个单线程线程池

用一个单线程顺序执行所有任务,无限队列

public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>(),
                                    threadFactory));
    }

二、创建一个固定大小的线程池

public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);
    }

三、创建一个无固定线程数量的线程池(仅有应急线程)

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }

 

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1, threadFactory));
    }

你好!对于基于 ExecutorService 线程池工具类的问题,我可以给你一些基本的解释和用法示例。 ExecutorService 是 Java 提供的一个线程池工具类,它可以帮助我们管理和执行线程任务。它的主要作用是重用线程,减少线程创建和销毁的开销,提高程序的性能。 首先,我们需要创建一个 ExecutorService 实例。可以使用 Executors 类的静态方法来创建不同类型的线程池,比如: ```java ExecutorService executorService = Executors.newFixedThreadPool(5); ``` 上面的代码创建了一个固定大小为 5 的线程池。你也可以根据需求选择其他类型的线程池,比如 CachedThreadPool、SingleThreadExecutor 等。 接下来,我们可以向线程池提交任务。可以通过调用 `submit()` 方法来提交一个 Callable 或 Runnable 对象,例如: ```java executorService.submit(new Callable<String>() { @Override public String call() throws Exception { // 在这里编写需要执行的任务逻辑 return "任务执行结果"; } }); ``` 或者使用 Lambda 表达式简化代码: ```java executorService.submit(() -> { // 在这里编写需要执行的任务逻辑 return "任务执行结果"; }); ``` 提交任务后,线程池会自动分配一个可用的线程去执行任务,并返回一个 Future 对象,可以用来获取任务的执行结果。 最后,当我们不再需要线程池时,可以调用 `shutdown()` 方法来关闭线程池: ```java executorService.shutdown(); ``` 这会等待已提交的任务执行完毕,并停止接受新的任务。 以上就是基于 ExecutorService 线程池工具类的基本用法。希望对你有所帮助!如果你还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值