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:

有效提供对象的一个类。

 * <ul>
 *   <li> Methods that create and return an {@link ExecutorService}
 *        set up with commonly useful configuration settings.

 *   <li> Methods that create and return a {@link ScheduledExecutorService}
 *        set up with commonly useful configuration settings.

 *   <li> Methods that create and return a "wrapped" ExecutorService, that
 *        disables reconfiguration by making implementation-specific methods
 *        inaccessible.
 *   <li> Methods that create and return a {@link ThreadFactory}
 *        that sets newly created threads to a known state.
 *   <li> 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}.
 * </ul>
 *
 * @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<Runnable>());
    }
    ----------
    //创建固定数量的核心线程数的线程池对象
    线程池的线程处于空闲状态也不会消失。线程池中最大数量减去核心数量的在空闲时间会消失。具体看代码传入的数据 和时间单位
   public static ExecutorService newFixedThreadPool(int nThreads) {
      return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
    }
    -----------------
    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(){}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值