Using a ThreadPool or just initializing a new Thread?

3

Let's say that the software all the time creates a new Runnable to perform a basic function every cycle, at the end of a day of execution I would have a few hundred Threads of this function created and finalized correctly by the JVM.

What advantages and disadvantages would I have in reusing a thread with, for example, Executors.newFixedThreadPool(n) compared to every time I create a new Runnable when I need to run?

What would I gain by reusing Threads or what would I not gain by reusing Threads?

    
asked by anonymous 04.04.2018 / 14:52

1 answer

3

Allocating Threads is a somewhat costly issue.

Instantiating a new thread requires a call to the operating system and a memory allocation (each thread has its own stack). Besides, if you unwittingly create too many threads, you can choke the CPU with it.

Changing from one thread to another is also a rather expensive process for the processor, each thread has its own context, and changing the context is a problem. So when you leave your Main Thread to create the new Thread, this can be a problem.

In a very simplified diagram, it works as follows:

The cost of switching these context switches is very high, and will happen every time you initialize a thread.

When you use a ThreadPool to perform your tasks, all Threads have already been allocated. Therefore, this cost has already been paid once. The allocated threads then look in a queue for any tasks to be performed (eg your Runnable).

Keeping a ThreadPool tends to be less costly than instantiating new Threads every time you perform an action.

However:

If the actions you perform are very sporadic (twice a day, for example), it may be more efficient to allocate a new Thread only when the action is performed.

ThreadPools are often used to perform actions that happen routinely (for example, serving Web pages to a client. This happens every time a client accesses the page, it happens several times)

Summarizing:

Creating new threads is very expensive. By using a ThreadPool, you considerably reduce this cost, making your application more performative, as well as avoiding competition problems due to poor implementation.

    
06.04.2018 / 18:01