• 第六章 Java newCachedThreadPool 示例


    在本教程中,我们将学习 Executor 的 newCachedThreadPool 工厂方法。 在上一篇教程中,我分享了 ThreadPoolExecutor 的介绍。如果您不了解 ThreadPoolExecutor 的概念,您应该先了解一下。

    Executor 的 newCachedThreadPool 工厂方法:

    此方法返回一个无界线程池。它将最大池大小设置为 Integer.Max,它将根据需要创建新线程。如果需求减少,如果线程空闲超过 1 分钟,它将关闭线程。

    例子:

    让我们创建一个任务。这里的任务是读取不同的文件并处理它们。

    1. package org.arpit.java2blog.bean;
    2. public class FetchDataFromFile implements Runnable{
    3. private final String fileName;
    4. public FetchDataFromFile(String fileName) {
    5. super();
    6. this.fileName = fileName;
    7. }
    8. @Override
    9. public void run() {
    10. try {
    11. System.out.println("Fetching data from "+fileName+" by "+Thread.currentThread().getName());
    12. Thread.sleep(5000); // Reading file
    13. System.out.println("Read file successfully: "+fileName+" by "+Thread.currentThread().getName());
    14. } catch (InterruptedException e) {
    15. e.printStackTrace();
    16. }
    17. }
    18. public String getFileName() {
    19. return fileName;
    20. }
    21. }

    让我们创建 ThreadPoolExecutor 它将消耗上述任务并处理它。

    1. package org.arpit.java2blog;
    2. import java.util.concurrent.Executors;
    3. import java.util.concurrent.ThreadPoolExecutor;
    4. public class ThreadPoolExecutorMain {
    5. public static void main(String args[]) {
    6. // Getting instance of ThreadPoolExecutor using Executors.newCachedThreadPool factory method
    7. ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    8. for (int i = 1; i <= 10; i++) {
    9. FetchDataFromFile fdff = new FetchDataFromFile("File :" + i);
    10. System.out.println("A new file has been added to read : " + fdff.getFileName());
    11. threadPoolExecutor.execute(fdff);
    12. }
    13. threadPoolExecutor.shutdown();
    14. }
    15. }

    当你运行上面的程序时,你会得到下面的输出:

    A new file has been added to read : File :1
    A new file has been added to read : File :2
    Fetching data from File :1 by pool-1-thread-1
    Fetching data from File :2 by pool-1-thread-2
    A new file has been added to read : File :3
    A new file has been added to read : File :4
    Fetching data from File :3 by pool-1-thread-3
    Fetching data from File :4 by pool-1-thread-4
    A new file has been added to read : File :5
    Fetching data from File :5 by pool-1-thread-5
    A new file has been added to read : File :6
    Fetching data from File :6 by pool-1-thread-6
    A new file has been added to read : File :7
    Fetching data from File :7 by pool-1-thread-7
    A new file has been added to read : File :8
    A new file has been added to read : File :9
    Fetching data from File :8 by pool-1-thread-8
    A new file has been added to read : File :10
    Fetching data from File :9 by pool-1-thread-9
    Fetching data from File :10 by pool-1-thread-10 

    如果你注意到,我们已经提交了 10 个任务,它根据需求创建了 10 个新线程。如果任何线程空闲超过一分钟,它就会将其拆除。当您想要比 newFixedThreadPool 更好的排队性能时,newCachedThreadPool 是一个不错的选择。如果要限制资源管理的并发任务数,请使用 newFixedThreadPool。

     LINK:第六章 Java newCachedThreadPool 示例 | 编程字典

  • 相关阅读:
    百度飞桨公布最新成果:凝聚535万开发者,服务20万家企事业单位
    打通“”任督二脉“”的大模型:基础大模型的进展意味着什么?变革的底层逻辑是什么?
    深入探索SDL游戏开发
    解决办法:AndroidStudio升级到android-studio-2021.3.1.16-windows.exe的坑
    MySQL(三)基本的SELECT语句
    高仿英雄联盟游戏网页制作作业 英雄联盟LOL游戏HTML网页设计模板 简单学生网页设计 静态HTML CSS网站制作成品
    Java编程学习-MySQL(数据库CRUD语句)
    linux和docker下mysql安装
    办公自动化杂志办公自动化杂志社办公自动化编辑部2022年第16期目录
    RegExp 对象
  • 原文地址:https://blog.csdn.net/JSPSEO/article/details/125883352