• 第六章 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 示例 | 编程字典

  • 相关阅读:
    Lumos-az/MiniSQL阅读笔记
    数据大屏指南 | 一屏看世界,一点选专业
    Python CT图像预处理——nii格式读取、重采样、窗宽窗位设置
    Lagrange Multipliers 拉格朗日乘数法(含 KKT 条件)
    防御安全第四次作业
    git中添加不上传的文件夹或文件的名字
    postgresql 之 数据目录内部结构 简介
    抽象工厂&责任链模式&观察者模式【Java】
    乐高式扩展:在Seal软件供应链防火墙中轻松集成代码规范工具
    GLPI资产管理系统安装Fusioninventory插件发现Windows和Linux主机
  • 原文地址:https://blog.csdn.net/JSPSEO/article/details/125883352