Question

Can anybody explain with examples about why should we use Thread-pools.

I have know about use of threadpools with Executors theoretically.

I have gone through number of tutorials, but I didn't get any practically examples about why should we use Threadpools, it can be newFixedThreadPool or newCachedThreadPool or newSingleThreadExecutor

in terms of scalability and performance .

If anybody explain me with respect to performance and scalability with examples about it?

Était-ce utile?

La solution

First off, check this description of thread pools that I wrote yesterday: Android Thread Pool to manage multiple bluetooth handeling threads? (ok, it was about android but it's the same for classic java).

The main use I always seem to find for using a threadpool is that is very nicely manages a very common problem: producer-consumer. In this pattern, someone needs to constantly send work items (the producer) to be processed by someone else (the consumers). The work items are obtained from some stream-like source, like a socket, a database, or a collection of disk files, and needs multiple workers in order to be processed efficiently. The main components identifiable here are:

  • the producer: a thread that keeps posting jobs
  • a queue where the jobs are posted
  • the consumers: worker threads that take jobs from the queue and execute them

In addition to this, synchronization needs to be employed to make all this work correctly, since reading and writing to the queue without synchronization can lead to corrupted and inconsistent data. Also, we need to make the system efficient, since the consumers should not waste CPU cycles when there is nothing to do.

Now this pattern is very common, but to implement it from scratch it takes a considerable effort, which is error prone and needs to be carefully reviewed.

The solution is the thread pool. It very conveniently manages the work queue, the consumer threads and all the synchronization needed. All you need to do is play the role of the producer and feed the pool with tasks!

Autres conseils

I would start with a problem and only then try to find a solution for it.

If you start the way you have, you can have a solution looking for a problem to solve and you are likely to use it inappropriately.

If you can't think of a use for thread pools, don't use them. ;)

A common mistake people make is to assume that because they have lots of cpus now, they have to use them all as if this were a reason in itself. Its like saying I have lots of disk space, I must find a way to use all of it.

A good reason to use thread pools is to improve the performance of CPU bounds processes and the simplicity of IO bound processes (rather than using non-blocking IO with one thread)

If you have a busy CPU bound process which performs tasks which can be executed independently you have a good use case for a thread pool.

Note: Thread pool often has just one thread. There are specific static factories for these. If you want a simple background worker, this may be an option.

Note 2: A common mistake is to assume that a CPU bound tasks will run best on hundreds or thousands of threads. The optimial number of threads can be the number of core or cpus you have. Once all these are busy, you may find additional threads just add overhead.

Initializing a new thread (and its own stack) is a costly operation.

Thread pools are use to avoid this cost by reusing threads already created. Thus using thread pools you get better performance then creating new threads every time.

Also note that created threads might need to be "deleted" after they have been used, which increases the cost of garbage collection and the frequency it will happen (as the memory fills up faster).

This analysis is just from the performance point of view. I cannot think of an advantage of using thread pools in terms of scalability at the moment.

I googled "why use java thread pools" and found:

A thread pool offers a solution to both the problem of thread life-cycle overhead and the problem of resource thrashing.

http://www.ibm.com/developerworks/library/j-jtp0730/index.html

and

The newCachedThreadPool method creates an executor with an expandable thread pool. This executor is suitable for applications that launch many short-lived tasks. The newSingleThreadExecutor method creates an executor that executes a single task at a time.

http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top