首页 > Java > java教程 > 正文

Java中的线程池

WBOY
发布: 2023-06-15 20:51:48
原创
1496人浏览过

java中,线程池被用来管理线程的创建、维护和销毁等任务。线程池中包含了一组线程和一个任务队列,当有任务需要执行时,线程池中的线程会自动获取任务并执行,任务执行完毕后,线程也会被回收到线程池中重复利用。

Java中的线程池API提供了一个Executors类来帮助我们创建线程池,提供了四种线程池的实现方式:FixedThreadPool、CachedThreadPool、SingleThreadExecutor和ScheduledThreadPool。

FixedThreadPool

固定大小的线程池,只有在工作线程数没有达到线程池大小的时候才会新建线程来执行任务。线程池可以通过构造函数指定最大线程数,如果没有指定,则默认为Integer.MAX_VALUE。

示例代码:

立即学习Java免费学习笔记(深入)”;

ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
    executorService.execute(()->{
        System.out.println(Thread.currentThread().getName()+" is executing task ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
}
登录后复制

运行结果:

pool-1-thread-1 is executing task 
pool-1-thread-3 is executing task 
pool-1-thread-5 is executing task 
pool-1-thread-2 is executing task 
pool-1-thread-4 is executing task 
pool-1-thread-5 is executing task 
pool-1-thread-3 is executing task 
pool-1-thread-1 is executing task 
pool-1-thread-2 is executing task 
pool-1-thread-4 is executing task 
登录后复制

CachedThreadPool

可缓存的线程池,当线程数超过了当前需要的数量时,会将多余的线程回收到线程池中,不需要的时候会自动销毁。如果线程池没有可用的线程,又有新的任务到来,线程池会创建一个新的线程来执行任务,直到线程池大小达到了Integer.MAX_VALUE的限制。

示例代码:

立即学习Java免费学习笔记(深入)”;

ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
    executorService.execute(()->{
        System.out.println(Thread.currentThread().getName()+" is executing task ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
}
登录后复制

运行结果:

pool-1-thread-1 is executing task 
pool-1-thread-2 is executing task 
pool-1-thread-3 is executing task 
pool-1-thread-4 is executing task 
pool-1-thread-5 is executing task 
pool-1-thread-6 is executing task 
pool-1-thread-7 is executing task 
pool-1-thread-8 is executing task 
pool-1-thread-9 is executing task 
pool-1-thread-10 is executing task 
登录后复制

SingleThreadExecutor

豆包AI编程
豆包AI编程

豆包推出的AI编程助手

豆包AI编程 483
查看详情 豆包AI编程

单线程的线程池,只有一个工作线程,可以保证所有任务按照指定的顺序来执行(FIFO、LIFO、优先级等),相当于一个特殊的FixedThreadPool。

示例代码:

立即学习Java免费学习笔记(深入)”;

ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
    executorService.execute(()->{
        System.out.println(Thread.currentThread().getName()+" is executing task ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
}
登录后复制

运行结果:

pool-1-thread-1 is executing task 
pool-1-thread-1 is executing task 
pool-1-thread-1 is executing task
......
登录后复制

ScheduledThreadPool

定时调度的线程池,可以按照指定的延迟时间或周期性地来执行任务,可以实现定时任务或者周期性任务。线程池的大小可以指定,如果没有指定则默认为Integer.MAX_VALUE。

示例代码:

立即学习Java免费学习笔记(深入)”;

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
ScheduledFuture<?> future = scheduledExecutorService.schedule(()->{
    System.out.println(Thread.currentThread().getName()+" is executing delay task ");
}, 5, TimeUnit.SECONDS);
scheduledExecutorService.scheduleAtFixedRate(()->{
    System.out.println(Thread.currentThread().getName()+" is executing periodic task ");
}, 2, 3, TimeUnit.SECONDS);
登录后复制

运行结果:

pool-1-thread-1 is executing periodic task 
pool-1-thread-2 is executing periodic task 
pool-1-thread-3 is executing periodic task 
pool-1-thread-1 is executing periodic task 
pool-1-thread-3 is executing periodic task 
pool-1-thread-2 is executing periodic task 
pool-1-thread-3 is executing periodic task 
pool-1-thread-2 is executing periodic task 
......
pool-1-thread-1 is executing delay task 
登录后复制

总结

线程池在多线程开发中是一个极其重要的概念,可以有效地减少线程的创建、销毁和上下文切换等开销,提升系统性能和可维护性。Java中提供了Executors类来方便地创建线程池,并提供了不同的实现方式来应对不同的应用场景。开发人员在使用线程池的时候需要根据具体的需求和负载情况,选择合适的实现方式来达到最佳的性能和效果。

以上就是Java中的线程池的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号