search

Home  >  Q&A  >  body text

java - ThreadPoolExecutor.execute()方法疑惑

根据类的继承机构,我们知道ThreadPoolExecutorService 继承了AbstractExecutorService类继承了ExecutorService接口继承了Executor接口中的execute()方法,这个方法是被用来专门执行异步任务的,通过传入参数Runnable来执行的。但是我看到ThreadPoolExecutorService.execute(Runnable command)方法中的源码表示不理解,求大牛帮忙看看这个源码,究竟都做了一些什么?

ringa_leeringa_lee2811 days ago795

reply all(2)I'll reply

  • 阿神

    阿神2017-04-18 09:37:56

    1. If there are threads smaller than corePoolSize (core thread) executing, the current task will be submitted to the first place. Then the addWorker method will do some checks;

    2. When a new task is put into the queue, it will be checked again (because the status will change or the thread pool is closed) to determine whether it needs to be rolled back or a new thread object needs to be created;

    3. If the task cannot be put into the queue, it will try to add a new Thread object, otherwise the task will be rejected

    PS: I remember reading a very good article that analyzed this method and the entire thread pool knowledge in detail. Let me help you find it...

    update: In-depth understanding of Java thread pool

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:37:56

    Come on

    reply
    0
  • Cancelreply