search

Home  >  Q&A  >  body text

java - ThreadPoolExecutor 得到正在运行的Thread实例

文档上只有得到 当前运行的线程数量

我要获得正在运行的,我放进去的runable的实例 怎么得到

巴扎黑巴扎黑2871 days ago810

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 11:34:44

    There should be no such support at the API level. One possible way is to override the beforeExecute and afterExecute methods of ThreadPoolExecutor. You can write the log file before/after the task is executed, and your monitoring process/thread can poll the log to get the current Running task.

    Or you can use some thread/inter-process communication methods (such as socket?) to get more real-time results.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 11:34:44

    It is true that beforeExecute and afterExecute can be overridden, and then a shared ConcurrentHashMap can be opened. BeforeExecute puts Runnable into HashMap, and afterExecute deletes Runnable from HashMap.

    reply
    0
  • Cancelreply