views:

139

answers:

4

Hi ,

Please explain Thread Pool and what is the use of Thread pool,please give me a real time example?

+2  A: 

Thread Pools from the Java Tutorials has a good overview:

Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead.

Justin Ethier
+3  A: 

A simple Google search will result in a wealth of information regarding Java thread pools and thread pools in general.

Here are some helpful links:

http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/essential/concurrency/pools.html http://en.wikipedia.org/wiki/Thread_pool_pattern

S73417H
Thanks for the tip. Hopefully this is now a more suitable answer.
S73417H
You're welcome and that's already better (previous comment removed). Still, I suggest to read [How to deal with google questions?](http://meta.stackoverflow.com/questions/8724/how-to-deal-with-google-questions), [Is it bad to ask google searchable questions on Stack Overflow](http://meta.stackoverflow.com/questions/33376/is-it-bad-to-ask-google-searchable-questions-on-stack-overflow) and [Is it appropriate to ask questions on Stack Overflow without prior research?](http://meta.stackoverflow.com/questions/23386/is-it-appropriate-to-ask-questions-on-stack-overflow-without-prior-research).
Pascal Thivent
Yes... Sorry for the somewhat facetious answer. It's early morning and the coffee has not yet kicked in. Thanks for the links also.
S73417H
+3  A: 

A thread pool is a group of threads initially created that waits for jobs and executes them. The idea is to have the threads always existing, so that we won't have to pay overhead time for creating them every time. They are appropriate when we know there's a stream of jobs to process, even though there could be some time when there are no jobs.

Here's a nice diagram from Wikipedia: alt text

Amir Rachum
+1  A: 

You may assume Threads to be actual workers and Thread Pools to be group of workers. You may create multiple groups for various reasons like priority, purpose, etc. So, while one pool may be for general purpose tasks like background schedules, email broadcasting, etc. there might be a transaction processing pool to simultaneously process multiple transactions. In case of an Executor Service, I am sure you would not like to delay the transactional jobs to be completed after other non-critical activities like broadcasting confirmation emails or database maintenance activities are not completed. You may segregate them into pools and maintain them independently. That's a very simplistic answer without getting into technical jargons. Regards, KT

Kapil