How Threadpool re-use Threads and how it works

后端 未结 4 1559
清酒与你
清酒与你 2020-11-30 03:17

My multithreading concepts are weak and trying to learn.

In Java what I know is, we can\'t call a thread more than once:

Thread t = new Thread; //So         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 03:40

    If there is no need to create new Thread in ThreadPool scenario, then how it works with same thread which just finished its run method, will that Thread can be used again?

    Simple - the original thread never actually completes. It just waits for another task to execute. In pseudo-code:

    // No, this isn't even slightly accurate! General impression only :)
    while (!pool.isShutdown()) {
        Runnable task = pool.waitForTaskOnQueue();
        task.run();
    }
    

    (Obviously when a thread pool is shut down, it would need to stop waiting threads from waiting for another task, too - but hopefully you get the general idea.)

提交回复
热议问题