In this post, we will see what is threadpool in Java along with an example, Thread Pool in Java Multithreading with Example | Threadpool Java | Executorservice in Java | thread pool in java,thread pool executor in java,thread pool,threadpoolexecutor in java,threadpool java,thread pool,threadpoolexecutor,thread pool in java multithreading,thread pool in java example,java thread pool,executor thread pool in java
Watch this video to know Threadpool in Java:
Watch on YouTube: https://www.youtube.com/watch?v=eeO2C6dfALc
What is Threadpool?
Threadpool is a concept in Java. It refers to the collection of threads i.e. a group of fixed size of threads.
How it works?
A thread is taken (i.e. pulled) from thread pool and task is allocated to it. Similarly, other threads are taken from thread pool and tasks are allocated to them. When task is completed, thread is returned to the thread pool. A returned thread in thread pool can be pulled back again and can be allocated a new task.
Advantage of Thread Pool:
Thread Pool reuses the threads. That's why, it reduces the time for creating new threads.
Java Thread Pool can be used with Servlet or JSP.
Go through the following example:
ThreadPoolExample.java
Output:
Check Thread Pool in Java with example of array addition i this link http://www.comrevo.com/2017/08/java-thread-pool-with-example-of-array-addition.html
Check Thread Pool in Java with example of matrix addition in this link http://www.comrevo.com/2017/08/java-thread-pool-with-example-of-matrix-addition.html
Check other posts on Multi-Threading in this link http://www.comrevo.com/2016/09/multi-threading.html
Watch this video to know Threadpool in Java:
Watch on YouTube: https://www.youtube.com/watch?v=eeO2C6dfALc
What is Threadpool?
Threadpool is a concept in Java. It refers to the collection of threads i.e. a group of fixed size of threads.
How it works?
A thread is taken (i.e. pulled) from thread pool and task is allocated to it. Similarly, other threads are taken from thread pool and tasks are allocated to them. When task is completed, thread is returned to the thread pool. A returned thread in thread pool can be pulled back again and can be allocated a new task.
Suppose there are three threads in a thread pool and five tasks.
(First Thread=First Task) First thread will be allocated first task. (Second Thread=Second Task) Second thread will be allocated second task. (Third Thread=Third Task) Third thread will be allocated third task. Once the first or second or third thread will be free i.e. completed task; it will return to thread pool and it will be allocated fourth task. Again whoever thread gets free will return back to thread pool and will be allocated fifth task. |
Thread Pool reuses the threads. That's why, it reduces the time for creating new threads.
Java Thread Pool can be used with Servlet or JSP.
Go through the following example:
ThreadPoolExample.java
import java.util.concurrent.*;
class WorkerThread implements Runnable
{
private int num;
public WorkerThread(int j)
{
this.num=j;
}
public void run()
{
System.out.println(Thread.currentThread().getName()+" (Start) num = "+num);
try {Thread.sleep(2000);} catch(Exception e) { }
System.out.println(Thread.currentThread().getName()+" (End)");
}
}
public class ThreadPoolExample
{
public static void main(String[] args)
{
ExecutorService executor = Executors.newFixedThreadPool(5);//creating a pool of 5 threads
for (int i = 0; i < 10; i++)
{
Runnable worker = new WorkerThread(i);
executor.execute(worker);//calling execute method of ExecutorService
}
executor.shutdown();//shutdown() will not allow allocating new tasks to threads but will wait till the completion of all allocated tasks
while (!executor.isTerminated()) { }
System.out.println("Finished all threads");
}
}
class WorkerThread implements Runnable
{
private int num;
public WorkerThread(int j)
{
this.num=j;
}
public void run()
{
System.out.println(Thread.currentThread().getName()+" (Start) num = "+num);
try {Thread.sleep(2000);} catch(Exception e) { }
System.out.println(Thread.currentThread().getName()+" (End)");
}
}
public class ThreadPoolExample
{
public static void main(String[] args)
{
ExecutorService executor = Executors.newFixedThreadPool(5);//creating a pool of 5 threads
for (int i = 0; i < 10; i++)
{
Runnable worker = new WorkerThread(i);
executor.execute(worker);//calling execute method of ExecutorService
}
executor.shutdown();//shutdown() will not allow allocating new tasks to threads but will wait till the completion of all allocated tasks
while (!executor.isTerminated()) { }
System.out.println("Finished all threads");
}
}
Output:
parag@parag-Inspiron-N4010:~/Desktop/JDBC$ javac ThreadPoolExample.java
parag@parag-Inspiron-N4010:~/Desktop/JDBC$ java ThreadPoolExample pool-1-thread-1 (Start) num = 0 pool-1-thread-5 (Start) num = 4 pool-1-thread-3 (Start) num = 2 pool-1-thread-4 (Start) num = 3 pool-1-thread-2 (Start) num = 1 pool-1-thread-1 (End) pool-1-thread-3 (End) pool-1-thread-1 (Start) num = 5 pool-1-thread-4 (End) pool-1-thread-5 (End) pool-1-thread-5 (Start) num = 8 pool-1-thread-4 (Start) num = 7 pool-1-thread-3 (Start) num = 6 pool-1-thread-2 (End) pool-1-thread-2 (Start) num = 9 pool-1-thread-1 (End) pool-1-thread-4 (End) pool-1-thread-3 (End) pool-1-thread-2 (End) pool-1-thread-5 (End) Finished all threads |
Check Thread Pool in Java with example of array addition i this link http://www.comrevo.com/2017/08/java-thread-pool-with-example-of-array-addition.html
Check Thread Pool in Java with example of matrix addition in this link http://www.comrevo.com/2017/08/java-thread-pool-with-example-of-matrix-addition.html
Check other posts on Multi-Threading in this link http://www.comrevo.com/2016/09/multi-threading.html
No comments:
Post a Comment