Java Thread Pool
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.
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.
Example of Java Thread Pool
Let's see a simple example of java thread pool using ExecutorService and Executors.
File: WorkerThread.java
File: JavaThreadPoolExample.java
Output:
pool-1-thread-1 (Start) message = 0
pool-1-thread-2 (Start) message = 1
pool-1-thread-3 (Start) message = 2
pool-1-thread-5 (Start) message = 4
pool-1-thread-4 (Start) message = 3
pool-1-thread-2 (End)
pool-1-thread-2 (Start) message = 5
pool-1-thread-1 (End)
pool-1-thread-1 (Start) message = 6
pool-1-thread-3 (End)
pool-1-thread-3 (Start) message = 7
pool-1-thread-4 (End)
pool-1-thread-4 (Start) message = 8
pool-1-thread-5 (End)
pool-1-thread-5 (Start) message = 9
pool-1-thread-2 (End)
pool-1-thread-1 (End)
pool-1-thread-4 (End)
pool-1-thread-3 (End)
pool-1-thread-5 (End)
Finished all threads
ThreadGroup in Java
Java provides a convenient way to group multiple threads in a single object. In such way, we can suspend, resume or interrupt group of threads by a single method call.
Note: Now suspend(), resume() and stop() methods are deprecated.
Java thread group is implemented by java.lang.ThreadGroup class.
Constructors of ThreadGroup class
There are only two constructors of ThreadGroup class.
No. | Constructor | Description |
---|---|---|
1) | ThreadGroup(String name) | creates a thread group with given name. |
2) | ThreadGroup(ThreadGroup parent, String name) | creates a thread group with given parent group and name. |
Important methods of ThreadGroup class
There are many methods in ThreadGroup class. A list of important methods are given below.
No. | Method | Description |
---|---|---|
1) | int activeCount() | returns no. of threads running in current group. |
2) | int activeGroupCount() | returns a no. of active group in this thread group. |
3) | void destroy() | destroys this thread group and all its sub groups. |
4) | String getName() | returns the name of this group. |
5) | ThreadGroup getParent() | returns the parent of this group. |
6) | void interrupt() | interrupts all threads of this group. |
7) | void list() | prints information of this group to standard console. |
Let's see a code to group multiple threads.
Now all 3 threads belong to one group. Here, tg1 is the thread group name, MyRunnable is the class that implements Runnable interface and "one", "two" and "three" are the thread names.
Now we can interrupt all threads by a single line of code only.
ThreadGroup Example
File: ThreadGroupDemo.java
Output:
one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
Thread[one,5,Parent ThreadGroup]
Thread[two,5,Parent ThreadGroup]
Thread[three,5,Parent ThreadGroup]
No comments:
Post a Comment