How Threadpool re-use Threads and how it works

后端 未结 4 1540
清酒与你
清酒与你 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

    So, considering above 3 steps, With Threadpool step 1 and Step 3 can be eliminated after fixed number of Thread Creation. only Step 2 for each task will be executed that is why Threadpool is faster? can we say like this? am I correct?

    Yes you are correct. Thread creation and destruction is one of the costly task. As in a thread pool threads are already created so the overhead of thread creation is not there. But if you have much more higher threads than it should have, it will be pretty bad for your application. It may go OutofMemorry or may be into some other issues. So to fix a thread pool size use the below formula:

    no of threads = 2 * no_of_cores * no_of_disks * percentage CPU utilization you need * (1 + (W/ C))

    (W/C) is the fraction stating Wait time to Compute time.

提交回复
热议问题