Reputation: 696
What is ThreadPool in java and What is the use of ThreadPool?
Is it will use shared memory and thread safe.
cab be declared thread priority with ThreadPool.
Upvotes: 1
Views: 611
Reputation: 696
Java Thread pool represents a group of worker threads that are waiting for the job and reuse many times.
In case of thread pool, a group of fixed size threads are created. A thread from the thread pool is pulled out and assigned a job by the service provider. After completion of the job, thread is contained in the thread pool again.
To understand this, consider a web server application where each HTTP request is handled by a separate thread. If the application simply creates a new thread for every new HTTP request, and the system receives more requests than it can handle immediately, the application will suddenly stop responding to all requests when the overhead of all those threads exceed the capacity of the system. With a limit on the number of the threads that can be created, the application will not be servicing HTTP requests as quickly as they come in, but it will be servicing them as quickly as the system can sustain.
Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks.
Advantage of Java Thread Pool Better performance It saves time because there is no need to create new thread.
Real time usage It is used in Servlet and JSP where container creates a thread pool to process the request.
Upvotes: 4