`
dwj147258
  • 浏览: 192105 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ThreadPoolExecutor机制

阅读更多

ThreadPoolExecutor机制 
一、概述 
1、ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程调度,线程池管理等等服务; 
2、Executors方法提供的线程服务,都是通过参数设置来实现不同的线程池机制。 
3、先来了解其线程池管理的机制,有助于正确使用,避免错误使用导致严重故障。同时可以根据自己的需求实现自己的线程池
 

二、核心构造方法讲解 
下面是ThreadPoolExecutor最核心的构造方法 

Java代码  收藏代码
  1. public ThreadPoolExecutor(int corePoolSize,  
  2.                               int maximumPoolSize,  
  3.                               long keepAliveTime,  
  4.                               TimeUnit unit,  
  5.                               BlockingQueue<Runnable> workQueue,  
  6.                               ThreadFactory threadFactory,  
  7.                               RejectedExecutionHandler handler) {  
  8.         if (corePoolSize < 0 ||  
  9.             maximumPoolSize <= 0 ||  
  10.             maximumPoolSize < corePoolSize ||  
  11.             keepAliveTime < 0)  
  12.             throw new IllegalArgumentException();  
  13.         if (workQueue == null || threadFactory == null || handler == null)  
  14.             throw new NullPointerException();  
  15.         this.corePoolSize = corePoolSize;  
  16.         this.maximumPoolSize = maximumPoolSize;  
  17.         this.workQueue = workQueue;  
  18.         this.keepAliveTime = unit.toNanos(keepAliveTime);  
  19.         this.threadFactory = threadFactory;  
  20.         this.handler = handler;  
  21.     }  


构造方法参数讲解 

参数名 作用
corePoolSize 核心线程池大小
maximumPoolSize 最大线程池大小
keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间;可以allowCoreThreadTimeOut(true)使得核心线程有效时间
TimeUnit keepAliveTime时间单位
workQueue 阻塞任务队列
threadFactory 新建线程工厂
RejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时,任务会交给RejectedExecutionHandler来处理



重点讲解: 
其中比较容易让人误解的是:corePoolSize,maximumPoolSize,workQueue之间关系。 

1.当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。 
2.当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行 
3.当workQueue已满,且maximumPoolSize>corePoolSize时,新提交任务会创建新线程执行任务 
4.当提交任务数超过maximumPoolSize时,新提交任务由RejectedExecutionHandler处理 
5.当线程池中超过corePoolSize线程,空闲时间达到keepAliveTime时,关闭空闲线程 
6.当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize线程空闲时间达到keepAliveTime也将关闭
 

线程管理机制图示: 


三、Executors提供的线程池配置方案 

1、构造一个固定线程数目的线程池,配置的corePoolSize与maximumPoolSize大小相同,同时使用了一个无界LinkedBlockingQueue存放阻塞任务,因此多余的任务将存在再阻塞队列,不会由RejectedExecutionHandler处理 

Java代码  收藏代码
  1. public static ExecutorService newFixedThreadPool(int nThreads) {  
  2.         return new ThreadPoolExecutor(nThreads, nThreads,  
  3.                                       0L, TimeUnit.MILLISECONDS,  
  4.                                       new LinkedBlockingQueue<Runnable>());  
  5.     }  


2、构造一个缓冲功能的线程池,配置corePoolSize=0,maximumPoolSize=Integer.MAX_VALUE,keepAliveTime=60s,以及一个无容量的阻塞队列 SynchronousQueue,因此任务提交之后,将会创建新的线程执行;线程空闲超过60s将会销毁 

Java代码  收藏代码
  1. public static ExecutorService newCachedThreadPool() {  
  2.         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,  
  3.                                       60L, TimeUnit.SECONDS,  
  4.                                       new SynchronousQueue<Runnable>());  
  5.     }  


3、构造一个只支持一个线程的线程池,配置corePoolSize=maximumPoolSize=1,无界阻塞队列LinkedBlockingQueue;保证任务由一个线程串行执行 

Java代码  收藏代码
  1. public static ExecutorService newSingleThreadExecutor() {  
  2.         return new FinalizableDelegatedExecutorService  
  3.             (new ThreadPoolExecutor(11,  
  4.                                     0L, TimeUnit.MILLISECONDS,  
  5.                                     new LinkedBlockingQueue<Runnable>()));  
  6.     }  


4、构造有定时功能的线程池,配置corePoolSize,无界延迟阻塞队列DelayedWorkQueue;有意思的是:maximumPoolSize=Integer.MAX_VALUE,由于DelayedWorkQueue是无界队列,所以这个值是没有意义的 

Java代码  收藏代码
  1. public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {  
  2.         return new ScheduledThreadPoolExecutor(corePoolSize);  
  3.     }  
  4.   
  5. public static ScheduledExecutorService newScheduledThreadPool(  
  6.             int corePoolSize, ThreadFactory threadFactory) {  
  7.         return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);  
  8.     }  
  9.   
  10. public ScheduledThreadPoolExecutor(int corePoolSize,  
  11.                              ThreadFactory threadFactory) {  
  12.         super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,  
  13.               new DelayedWorkQueue(), threadFactory);  
  14.     }  



四、定制属于自己的非阻塞线程池 

Java代码  收藏代码
  1. import java.util.concurrent.ArrayBlockingQueue;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.RejectedExecutionHandler;  
  4. import java.util.concurrent.ThreadFactory;  
  5. import java.util.concurrent.ThreadPoolExecutor;  
  6. import java.util.concurrent.TimeUnit;  
  7. import java.util.concurrent.atomic.AtomicInteger;  
  8.   
  9.   
  10. public class CustomThreadPoolExecutor {  
  11.   
  12.       
  13.     private ThreadPoolExecutor pool = null;  
  14.       
  15.       
  16.     /** 
  17.      * 线程池初始化方法 
  18.      *  
  19.      * corePoolSize 核心线程池大小----10 
  20.      * maximumPoolSize 最大线程池大小----30 
  21.      * keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间----30+单位TimeUnit 
  22.      * TimeUnit keepAliveTime时间单位----TimeUnit.MINUTES 
  23.      * workQueue 阻塞队列----new ArrayBlockingQueue<Runnable>(10)====10容量的阻塞队列 
  24.      * threadFactory 新建线程工厂----new CustomThreadFactory()====定制的线程工厂 
  25.      * rejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时, 
  26.      *                          即当提交第41个任务时(前面线程都没有执行完,此测试方法中用sleep(100)), 
  27.      *                                任务会交给RejectedExecutionHandler来处理 
  28.      */  
  29.     public void init() {  
  30.         pool = new ThreadPoolExecutor(  
  31.                 10,  
  32.                 30,  
  33.                 30,  
  34.                 TimeUnit.MINUTES,  
  35.                 new ArrayBlockingQueue<Runnable>(10),  
  36.                 new CustomThreadFactory(),  
  37.                 new CustomRejectedExecutionHandler());  
  38.     }  
  39.   
  40.       
  41.     public void destory() {  
  42.         if(pool != null) {  
  43.             pool.shutdownNow();  
  44.         }  
  45.     }  
  46.       
  47.       
  48.     public ExecutorService getCustomThreadPoolExecutor() {  
  49.         return this.pool;  
  50.     }  
  51.       
  52.     private class CustomThreadFactory implements ThreadFactory {  
  53.   
  54.         private AtomicInteger count = new AtomicInteger(0);  
  55.           
  56.         @Override  
  57.         public Thread newThread(Runnable r) {  
  58.             Thread t = new Thread(r);  
  59.             String threadName = CustomThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);  
  60.             System.out.println(threadName);  
  61.             t.setName(threadName);  
  62.             return t;  
  63.         }  
  64.     }  
  65.       
  66.       
  67.     private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {  
  68.   
  69.         @Override  
  70.         public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {  
  71.             // 记录异常  
  72.             // 报警处理等  
  73.             System.out.println("error.............");  
  74.         }  
  75.     }  
  76.       
  77.       
  78.       
  79.     // 测试构造的线程池  
  80.     public static void main(String[] args) {  
  81.         CustomThreadPoolExecutor exec = new CustomThreadPoolExecutor();  
  82.         // 1.初始化  
  83.         exec.init();  
  84.           
  85.         ExecutorService pool = exec.getCustomThreadPoolExecutor();  
  86.         for(int i=1; i<100; i++) {  
  87.             System.out.println("提交第" + i + "个任务!");  
  88.             pool.execute(new Runnable() {  
  89.                 @Override  
  90.                 public void run() {  
  91.                     try {  
  92.                         Thread.sleep(3000);  
  93.                     } catch (InterruptedException e) {  
  94.                         e.printStackTrace();  
  95.                     }  
  96.                     System.out.println("running=====");  
  97.                 }  
  98.             });  
  99.         }  
  100.           
  101.           
  102.           
  103.         // 2.销毁----此处不能销毁,因为任务没有提交执行完,如果销毁线程池,任务也就无法执行了  
  104.         // exec.destory();  
  105.           
  106.         try {  
  107.             Thread.sleep(10000);  
  108.         } catch (InterruptedException e) {  
  109.             e.printStackTrace();  
  110.         }  
  111.     }  
  112. }  


方法中建立一个核心线程数为30个,缓冲队列有10个的线程池。每个线程任务,执行时会先睡眠3秒,保证提交10任务时,线程数目被占用完,再提交30任务时,阻塞队列被占用完,,这样提交第41个任务是,会交给CustomRejectedExecutionHandler 异常处理类来处理。 

提交任务的代码如下: 

Java代码  收藏代码
  1. public void execute(Runnable command) {  
  2.         if (command == null)  
  3.             throw new NullPointerException();  
  4.         /* 
  5.          * Proceed in 3 steps: 
  6.          * 
  7.          * 1. If fewer than corePoolSize threads are running, try to 
  8.          * start a new thread with the given command as its first 
  9.          * task.  The call to addWorker atomically checks runState and 
  10.          * workerCount, and so prevents false alarms that would add 
  11.          * threads when it shouldn't, by returning false. 
  12.          * 
  13.          * 2. If a task can be successfully queued, then we still need 
  14.          * to double-check whether we should have added a thread 
  15.          * (because existing ones died since last checking) or that 
  16.          * the pool shut down since entry into this method. So we 
  17.          * recheck state and if necessary roll back the enqueuing if 
  18.          * stopped, or start a new thread if there are none. 
  19.          * 
  20.          * 3. If we cannot queue task, then we try to add a new 
  21.          * thread.  If it fails, we know we are shut down or saturated 
  22.          * and so reject the task. 
  23.          */  
  24.         int c = ctl.get();  
  25.         if (workerCountOf(c) < corePoolSize) {  
  26.             if (addWorker(command, true))  
  27.                 return;  
  28.             c = ctl.get();  
  29.         }  
  30.         if (isRunning(c) && workQueue.offer(command)) {  
  31.             int recheck = ctl.get();  
  32.             if (! isRunning(recheck) && remove(command))  
  33.                 reject(command);  
  34.             else if (workerCountOf(recheck) == 0)  
  35.                 addWorker(nullfalse);  
  36.         }  
  37.         else if (!addWorker(command, false))  
  38.             reject(command);  
  39.     }  


注意:41以后提交的任务就不能正常处理了,因为,execute中提交到任务队列是用的offer方法,如上面代码,这个方法是非阻塞的,所以就会交给CustomRejectedExecutionHandler 来处理,所以对于大数据量的任务来说,这种线程池,如果不设置队列长度会OOM,设置队列长度,会有任务得不到处理,接下来我们构建一个阻塞的自定义线程池 

五、定制属于自己的阻塞线程池 

Java代码  收藏代码
  1. package com.tongbanjie.trade.test.commons;  
  2.   
  3. import java.util.concurrent.ArrayBlockingQueue;  
  4. import java.util.concurrent.ExecutorService;  
  5. import java.util.concurrent.RejectedExecutionHandler;  
  6. import java.util.concurrent.ThreadFactory;  
  7. import java.util.concurrent.ThreadPoolExecutor;  
  8. import java.util.concurrent.TimeUnit;  
  9. import java.util.concurrent.atomic.AtomicInteger;  
  10.   
  11. public class CustomThreadPoolExecutor {    
  12.         
  13.         
  14.     private ThreadPoolExecutor pool = null;    
  15.         
  16.         
  17.     /**  
  18.      * 线程池初始化方法  
  19.      *   
  20.      * corePoolSize 核心线程池大小----1  
  21.      * maximumPoolSize 最大线程池大小----3  
  22.      * keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间----30+单位TimeUnit  
  23.      * TimeUnit keepAliveTime时间单位----TimeUnit.MINUTES  
  24.      * workQueue 阻塞队列----new ArrayBlockingQueue<Runnable>(5)====5容量的阻塞队列  
  25.      * threadFactory 新建线程工厂----new CustomThreadFactory()====定制的线程工厂  
  26.      * rejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时,  
  27.      *                          即当提交第41个任务时(前面线程都没有执行完,此测试方法中用sleep(100)),  
  28.      *                                任务会交给RejectedExecutionHandler来处理  
  29.      */    
  30.     public void init() {    
  31.         pool = new ThreadPoolExecutor(    
  32.                 1,    
  33.                 3,    
  34.                 30,    
  35.                 TimeUnit.MINUTES,    
  36.                 new ArrayBlockingQueue<Runnable>(5),    
  37.                 new CustomThreadFactory(),    
  38.                 new CustomRejectedExecutionHandler());    
  39.     }    
  40.     
  41.         
  42.     public void destory() {    
  43.         if(pool != null) {    
  44.             pool.shutdownNow();    
  45.         }    
  46.     }    
  47.         
  48.         
  49.     public ExecutorService getCustomThreadPoolExecutor() {    
  50.         return this.pool;    
  51.     }    
  52.         
  53.     private class CustomThreadFactory implements ThreadFactory {    
  54.     
  55.         private AtomicInteger count = new AtomicInteger(0);    
  56.             
  57.         @Override    
  58.         public Thread newThread(Runnable r) {    
  59.             Thread t = new Thread(r);    
  60.             String threadName = CustomThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);    
  61.             System.out.println(threadName);    
  62.             t.setName(threadName);    
  63.             return t;    
  64.         }    
  65.     }    
  66.         
  67.         
  68.     private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {    
  69.     
  70.         @Override    
  71.         public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {    
  72.             try {  
  73.                                 // 核心改造点,由blockingqueue的offer改成put阻塞方法  
  74.                 executor.getQueue().put(r);  
  75.             } catch (InterruptedException e) {  
  76.                 e.printStackTrace();  
  77.             }  
  78.         }    
  79.     }    
  80.         
  81.         
  82.         
  83.     // 测试构造的线程池    
  84.     public static void main(String[] args) {    
  85.           
  86.         CustomThreadPoolExecutor exec = new CustomThreadPoolExecutor();    
  87.         // 1.初始化    
  88.         exec.init();    
  89.             
  90.         ExecutorService pool = exec.getCustomThreadPoolExecutor();    
  91.         for(int i=1; i<100; i++) {    
  92.             System.out.println("提交第" + i + "个任务!");    
  93.             pool.execute(new Runnable() {    
  94.                 @Override    
  95.                 public void run() {    
  96.                     try {    
  97.                         System.out.println(">>>task is running=====");   
  98.                         TimeUnit.SECONDS.sleep(10);  
  99.                     } catch (InterruptedException e) {    
  100.                         e.printStackTrace();    
  101.                     }    
  102.                 }    
  103.             });    
  104.         }    
  105.             
  106.             
  107.         // 2.销毁----此处不能销毁,因为任务没有提交执行完,如果销毁线程池,任务也就无法执行了    
  108.         // exec.destory();    
  109.             
  110.         try {    
  111.             Thread.sleep(10000);    
  112.         } catch (InterruptedException e) {    
  113.             e.printStackTrace();    
  114.         }    
  115.     }    
  116. }    



解释:当提交任务被拒绝时,进入拒绝机制,我们实现拒绝方法,把任务重新用阻塞提交方法put提交,实现阻塞提交任务功能,防止队列过大,OOM,提交被拒绝方法在下面 

   

Java代码  收藏代码
  1. public void execute(Runnable command) {  
  2.         if (command == null)  
  3.             throw new NullPointerException();  
  4.   
  5.         int c = ctl.get();  
  6.         if (workerCountOf(c) < corePoolSize) {  
  7.             if (addWorker(command, true))  
  8.                 return;  
  9.             c = ctl.get();  
  10.         }  
  11.         if (isRunning(c) && workQueue.offer(command)) {  
  12.             int recheck = ctl.get();  
  13.             if (! isRunning(recheck) && remove(command))  
  14.                 reject(command);  
  15.             else if (workerCountOf(recheck) == 0)  
  16.                 addWorker(nullfalse);  
  17.         }  
  18.         else if (!addWorker(command, false))  
  19.             // 进入拒绝机制, 我们把runnable任务拿出来,重新用阻塞操作put,来实现提交阻塞功能  
  20.             reject(command);  
  21.     }  




总结: 
1、用ThreadPoolExecutor自定义线程池,看线程是的用途,如果任务量不大,可以用无界队列,如果任务量非常大,要用有界队列,防止OOM 
2、如果任务量很大,还要求每个任务都处理成功,要对提交的任务进行阻塞提交,重写拒绝机制,改为阻塞提交。保证不抛弃一个任务 
3、最大线程数一般设为2N+1最好,N是CPU核数 
4、核心线程数,看应用,如果是任务,一天跑一次,设置为0,合适,因为跑完就停掉了,如果是常用线程池,看任务量,是保留一个核心还是几个核心线程数 
5、如果要获取任务执行结果,用CompletionService,但是注意,获取任务的结果的要重新开一个线程获取,如果在主线程获取,就要等任务都提交后才获取,就会阻塞大量任务结果,队列过大OOM,所以最好异步开个线程获取结果

 

在Spring中使用自定义的线程池可以这样配置

<bean id="springTaskExecutor"
          class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="3"/>
        <property name="maxPoolSize" value="6"/>
        <property name="queueCapacity" value="1024"/>
    </bean>

 这样在代码中只需这样引用

private TaskExecutor  taskExecutor ;

将springTaskExecutor注入到其中即可

分享到:
评论

相关推荐

    ThreadPoolExecutor运转机制介绍

    ### ThreadPoolExecutor 运转机制详解 #### 一、ThreadPoolExecutor 的基本概念与构造函数解析 在Java并发编程中,`ThreadPoolExecutor` 是一种强大的工具,它可以帮助开发者有效地管理和执行线程。`...

    线程池之ThreadPoolExecutor.docx

    线程池的工作机制在于控制线程数量,它会将任务放入队列,然后根据线程池的设定创建并启动线程执行这些任务。如果线程数量超过最大限制,额外的任务会被排队等待,直到有线程完成任务释放资源。线程池的使用有三个...

    JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用

    该类提供了一个灵活的线程池管理机制,允许开发者根据需要创建和管理线程池。ThreadPoolExecutor类的构造方法为: ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ...

    java ThreadPoolExecutor 并发调用实例详解

    在这些机制中,ThreadPoolExecutor 是一个非常重要的概念,它提供了一个线程池,允许开发者将任务提交到线程池中,并行地执行这些任务。 ThreadPoolExecutor 的主要作用是管理一个线程池,线程池中包含多个线程,每...

    JDK之ThreadPoolExecutor源码分析1

    ThreadPoolExecutor作为Java中的线程池实现,其内部机制相当复杂且灵活。本文将深入解析ThreadPoolExecutor的execute()方法执行流程,以帮助我们理解线程池的工作原理。 当一个任务被提交到线程池,线程池的执行...

    使用线程池ThreadPoolExecutor 抓取论坛帖子列表

    在IT行业中,线程池是多线程编程中一个重要的概念,它可以帮助我们高效地管理和控制并发执行的任务。...通过深入理解线程池的工作机制和源码,我们可以更好地设计和优化我们的并发程序,实现高效的数据抓取。

    ThreadPoolExecutor线程池的使用方法

    ThreadPoolExecutor线程池提供了灵活的线程管理机制,可以根据需要选择合适的任务队列和拒绝策略,并且可以自定义线程工厂和线程工具类,从而满足不同应用场景的需求。 ThreadPoolExecutor的使用方法可以分为以下几...

    线程池ThreadPoolExecutor原理源码分析.md

    本文将围绕 `ThreadPoolExecutor` 的核心方法 `execute()` 进行深入解析,帮助读者更好地理解其内部机制。 #### 二、构造方法 `ThreadPoolExecutor` 提供了一个构造函数,用于初始化线程池: ```java public ...

    说说你对ThreadPoolExecutor的理解.docx

    总的来说,ThreadPoolExecutor提供了一种高效、灵活的线程管理机制,可以控制并发程度,避免资源浪费,同时能优雅地处理任务提交和执行过程中的各种情况。理解和熟练使用ThreadPoolExecutor对于编写高性能的多线程...

    java 中ThreadPoolExecutor原理分析

    ThreadPoolExecutor 是 Java 并发编程中的一种高级线程池实现,它提供了一个灵活的线程池管理机制,允许开发者根据需要配置线程池的参数以满足不同的需求。在这篇文章中,我们将深入探讨 ThreadPoolExecutor 的原理...

    12-线程池ThreadPoolExecutor底层原理源码分析(下)-周瑜.pdf

    通过上述对`ThreadPoolExecutor`线程池底层实现原理的解析,我们可以看到Java线程池的强大之处在于其高效的状态管理和任务调度机制。通过对`ctl`变量的巧妙利用,线程池能够有效地管理线程状态和数量,从而实现高...

    11-线程池ThreadPoolExecutor底层原理源码分析(上)-周瑜.pdf

    根据提供的文件信息,我们...通过上面的介绍,我们可以了解到`ThreadPoolExecutor`在Java并发编程中的重要性和其内部机制。合理配置线程池不仅可以提高系统的响应速度,还可以有效利用系统资源,减少不必要的资源浪费。

    Java线程池ThreadPoolExecutor原理及使用实例

    Java线程池ThreadPoolExecutor是Java并发编程中的一种基本机制,主要用于管理和执行任务的线程池。下面对其原理和使用实例进行详细介绍。 线程池概述 线程池是一个池子,负责管理和执行任务的线程。当用户提交任务...

    试析Android异步通信机制.pdf

    在Android应用开发中,异步通信机制扮演着至关重要的角色,它使得应用程序可以在不阻塞用户界面的情况下执行耗时操作,如网络请求、数据库查询或大型数据处理。本篇分析将深入探讨Android异步通信的几种主要实现方式...

    java线程池和反射机制例子

    Java中,`java.util.concurrent`包下的`ExecutorService`接口和`ThreadPoolExecutor`类提供了线程池的相关功能。我们可以自定义线程池的大小、工作队列、拒绝策略等参数,以适应不同的并发需求。例如,通过`...

    顶层接口Executors详解

    顶层接口Executors详解 Executors框架是Java语言中用于异步执行任务的高级接口,...ThreadPoolExecutor提供了一个灵活的线程池管理机制,可以根据系统的负荷情况动态地调整线程池的大小,提高系统的性能和可扩展性。

    android学习笔记之消息机制,异步和多线程[参考].pdf

    对于更复杂的多线程需求,可以使用`ThreadPoolExecutor`来创建和管理线程池,从而更好地控制线程的数量和生命周期。线程池可以有效利用系统资源,减少频繁创建和销毁线程的开销。此外,`IntentService`是一种单线程...

    Android线程间 massage 机制

    为了提高性能,可以使用AsyncTask、IntentService或ThreadPoolExecutor等其他并发工具。对于耗时操作,推荐在工作线程中执行,通过Handler向主线程发送更新UI的消息,避免主线程阻塞。 总的来说,理解并熟练运用...

    jtp-JohanThreadPool新版

    JohanThreadPool最早起源于jdk-...尽管JohanThreadPool封装上比ThreadPoolExecutor更加复杂,但是核心数据结构简单很多,运行机制也非常简洁;简单总是没错的,而且测试也说明了问题,性能上优于ThreadPoolExecutor。

Global site tag (gtag.js) - Google Analytics