Java线程池ThreadPool简单介绍
发布日期:2021-05-12 16:26:15 浏览次数:31 分类:精选文章

本文共 4067 字,大约阅读时间需要 13 分钟。

���������������

������������������

���������������Java������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������CPU���������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������
  • ������������������

    ���������������������

    public ThreadPoolExecutor(
    int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue
    workQueue,
    ThreadFactory threadFactory,
    RejectedExecutionHandler handler)
    {
    if (corePoolSize < 0 ||
    maximumPoolSize <= 0 ||
    maximumPoolSize < corePoolSize ||
    keepAliveTime < 0)
    {
    throw new IllegalArgumentException();
    }
    if (workQueue == null || threadFactory == null || handler == null)
    {
    throw new NullPointerException();
    }
    // ���������������������...
    }

    ���������������������������

    • corePoolSize���������������������������������������
    • maximumPoolSize������������������������������ ��������������� = ��������������� + ���������������������
    • keepAliveTime���������������������������������������
    • unit���������������������
    • workQueue������������������������������
    • threadFactory������������������������������
    • handler���������������������������

    ������������������

  • ������������������������������������������������������������������������������������������������������corePoolSize���������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������workQueue���������������
  • ���������������������������������������������������������������maximumPoolSize���������������������������������������������������handler���������������������
  • ������������types

  • SynchronousQueue���������������������������������������������������������������������������������������������������������������������������������
  • LinkedBlockingQueue���������������������������������������������������������������������������������������������������
  • ArrayBlockingQueue���������������������������������������������������������������������������������������������������
  • DelayQueue������������������Delayed���������������������������������������������������
  • ������������

    ���������������������

  • AbortPolicy���������������������������������
  • DiscardPolicy������������������������
  • DiscardOldestPolicy������������������������������������������������������
  • CallerRunsPolicy������������������������������������������������������
  • ���������������

  • FixedThreadPool���������������������������������������������������������������������
  • public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
    }
    1. CachedThreadPool������������������������������������������������������������������
    2. public static ExecutorService newCachedThreadPool() {
      return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>());
      }
      1. SingleThreadExecutor���������������������������������������
      2. public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()));
        }
        1. ScheduledThreadPool���������������������������������������������
        2. public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
          return new ScheduledThreadPoolExecutor(corePoolSize);
          }

          ���������������������������

          ������������������������������������������������������������������������������������

          • ���������������������������������I/O������������������������CachedThreadPool���������������������������������FixedThreadPool���
    上一篇:AQS 简单介绍
    下一篇:ThreadLocal原理简介和内存泄漏

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2025年05月01日 14时39分24秒