当前位置: 代码网 > it编程>编程语言>Java > 线程池之jdk1.8 Executors创建线程池的几种方式

线程池之jdk1.8 Executors创建线程池的几种方式

2024年08月06日 Java 我要评论
1、newfixedthreadpool - 定长线程池创建一个线程池,该线程池重用在共享无界队列上运行的固定数量的线程。在任何时候,线程最多都是活动的处理任务。如果在所有线程都处于活动状态时提交其他

1、newfixedthreadpool - 定长线程池

创建一个线程池,该线程池重用在共享无界队列上运行的固定数量的线程。

在任何时候,线程最多都是活动的处理任务。如果在所有线程都处于活动状态时提交其他任务,它们将在队列中等待,直到有线程可用。

如果任何线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将有一个新线程替代它。

池中的线程将一直存在,直到显式关闭。

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);
}

2、newsinglethreadexecutor - 单一线程池

创建一个执行器,该执行器使用一个工作线程在无界队列上运行。

(但是请注意,如果此单线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将使用一个新线程代替它。)

任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。

与其他等效的newfixedthreadpool(1)不同,返回的执行器保证不可重新配置以使用其他线程。

    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));
    }

newsinglethreadexecutor和newfixedthreadpool(1)区别

3、newcachedthreadpool - 缓存线程池

创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用这些线程。

这些池通常会提高执行许多短期异步任务的程序的性能。

调用execute将重用以前构造的线程(如果可用)。

如果没有可用的现有线程,将创建一个新线程并将其添加到池中。

60秒未使用的线程将被终止并从缓存中删除。

因此,闲置足够长时间的池不会消耗任何资源。

请注意,可以使用threadpoolexecutor构造函数创建具有类似属性但不同细节(例如超时参数)的池。

    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);
    }

4、newscheduledthreadpool - 调度线程池

创建一个线程池,该线程池可以安排命令在给定延迟后运行,或定期执行。

参数:

corepoolsize – 池中要保留的线程数,即使它们处于空闲状态

    public static scheduledexecutorservice newscheduledthreadpool(int corepoolsize) {
        return new scheduledthreadpoolexecutor(corepoolsize);
    }
    public static scheduledexecutorservice newscheduledthreadpool(
            int corepoolsize, threadfactory threadfactory) {
        return new scheduledthreadpoolexecutor(corepoolsize, threadfactory);
    }

5、newsinglethreadscheduledexecutor - 单线程调度线程池

创建一个单线程执行器,该执行器可以安排命令在给定延迟后运行,或定期执行。

(但是请注意,如果此单线程在关机前的执行过程中由于故障而终止,那么如果需要执行后续任务,将使用一个新线程代替它。)

任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。

与其他等效的newscheduledthreadpool(1)不同,返回的执行器保证不可重新配置以使用其他线程。

    public static scheduledexecutorservice newsinglethreadscheduledexecutor() {
        return new delegatedscheduledexecutorservice
            (new scheduledthreadpoolexecutor(1));
    }
    public static scheduledexecutorservice newsinglethreadscheduledexecutor(threadfactory threadfactory) {
        return new delegatedscheduledexecutorservice
            (new scheduledthreadpoolexecutor(1, threadfactory));
    }

6、newworkstealingpool - 抢占操作线程池

创建一个线程池,该线程池维护足够多的线程以支持给定的并行度级别,并且可以使用多个队列来减少争用。

并行级别对应于积极参与或可参与任务处理的最大线程数。

线程的实际数量可能会动态增长和收缩。

工作窃取池不保证提交任务的执行顺序。

参数:

并行度——目标并行度级别

    public static executorservice newworkstealingpool(int parallelism) {
        return new forkjoinpool
            (parallelism,
             forkjoinpool.defaultforkjoinworkerthreadfactory,
             null, true);
    }
    public static executorservice newworkstealingpool() {
        return new forkjoinpool
            (runtime.getruntime().availableprocessors(),
             forkjoinpool.defaultforkjoinworkerthreadfactory,
             null, true);
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com