zoukankan      html  css  js  c++  java
  • Executors 构建线程池

    Executors包含一系列静态方法,可以用于构建线程池。

    返回实现了 ExecutorService 接口的对象:

    • newCachedThreadPool
    • newFixedThreadPool(int threads)
    • newSingleThreadPool

    返回实现了ScheduledExecutorService接口的对象

    • newScheduledThreadPool(int threads)
    • newScheduledSingleThreadPool

    当用完一个线程池时,要调用shutdown或shutdownNow。

    • shutdown方法启动该线程池的关闭序列,执行器不再接受新任务。当所有任务完成后,线程池中的线程死亡。
    • showdownNow方法取消尚未开始的任务并试图中断正在运行的线程。

    总结使用线程池需要做的事情:

      调用Executor的类方法newCachedThreadPool等;
      调用submit提交Runnable或Callable对象;
      如果想取消任务,或者提交的是Callable对象,要保存好Future对象;
      当不再提交任务时,调用shutdown方法。

  • 相关阅读:
    237. 删除链表中的节点
    牛客网-第一场-J-Fraction Comparision
    1. 两数之和
    CCF-201903-1大中小
    学习Python
    Convert Sorted Array to Binary Search Tree
    3-1
    Merge Sorted Array
    Climbing Stairs
    Add Binary
  • 原文地址:https://www.cnblogs.com/yitong0768/p/4557277.html
Copyright © 2011-2022 走看看