java 线程池常见分类

线程池相关知识

/**
 * Factory and utility methods for {@link Executor}, {@link
 * ExecutorService}, {@link ScheduledExecutorService}, {@link
 * ThreadFactory}, and {@link Callable} classes defined in this
 * package. This class supports the following kinds of methods:

有效提供对象的一个类。

 * 
    *
  • Methods that create and return an {@link ExecutorService} * set up with commonly useful configuration settings. *
  • Methods that create and return a {@link ScheduledExecutorService} * set up with commonly useful configuration settings. *
  • Methods that create and return a "wrapped" ExecutorService, that * disables reconfiguration by making implementation-specific methods * inaccessible. *
  • Methods that create and return a {@link ThreadFactory} * that sets newly created threads to a known state. *
  • Methods that create and return a {@link Callable} * out of other closure-like forms, so they can be used * in execution methods requiring {@code Callable}. *
* * @since 1.5 * @author Doug Lea */ //继承和实现关系 public interface Executor public interface ExecutorService extends Executor public abstract class AbstractExecutorService implements ExecutorService public class ThreadPoolExecutor extends AbstractExecutorService public interface ScheduledExecutorService extends ExecutorService static class FinalizableDelegatedExecutorService extends DelegatedExecutorService static class DelegatedScheduledExecutorService extends DelegatedExecutorService implements ScheduledExecutorService static class DelegatedExecutorService extends AbstractExecutorService ------ //创建一个非核心线程的线程池,空闲线程只有60秒的生命周期时间,如果有空闲线程存活,就用空闲线程执行任务。 否则就创建新的线程执行任务 public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS, new SynchronousQueue()); } ---------- //创建固定数量的核心线程数的线程池对象 线程池的线程处于空闲状态也不会消失。线程池中最大数量减去核心数量的在空闲时间会消失。具体看代码传入的数据 和时间单位 public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue()); } ----------------- Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically. //创建一个线程池可以执行定时任务和具有固定周期的重复任务 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } --------------------------------- Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent {@code newFixedThreadPool(1)} the returned executor is guaranteed not to be reconfigurable to use additional threads. //创建一个线程池只有一个工作线程去执行所有的任务,当需要的时候用提供的ThreadFactory 创建一个线程,(注意 如果这个线程在执行期间失败,新的线程会替代他执行随后的任务, 保证所有的任务顺序执行,并且保证在给定的时间只有一个任务执行,不同于 newFixedThreadPool(1) 返回的线程池对象,它不能增加更多的线程对象 ) Executors.newSingleThreadExecutor(); ------------------------------

public class Executors {
//不能实例化
private Executors(){}

你可能感兴趣的:(Android)