If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.)
Now, Take a simple example,
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(5, 10, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50));
Here, 5 is the corePoolSize - means Jvm will create new thread for new task for first 5 tasks. and other tasks will be added to the queue until queue is getting full (50 tasks).
10 is the maxPoolSize - JVM can create max 10 threads. Means if there are already 5 task/thread is running and queue is full with 50 pending tasks and if one more new request/task is arriving in queue then JVM will create new thread up to 10 (total threads=previous 5 + new 5);
new ArrayBlockingQueue(50) = is a total queue size - it can queue 50 tasks in it.
once all 10 threads are running and if new task is arriving then that new task will be rejected.
Rules for creating Threads internally by SUN:
If the number of threads is less than the corePoolSize, create a new Thread to run a new task.
If the number of threads is equal (or greater than) the corePoolSize, put the task into the queue.
If the queue is full, and the number of threads is less than the maxPoolSize, create a new thread to run tasks in.
If the queue is full, and the number of threads is greater than or equal to maxPoolSize, reject the task.
Core Pool Size--> its the core of pool which can support the number of tasks alone by creating threads
Max Pool Size--> This is the sum of threads Core pool has and number of threads it can create more if all threads of Core Pool are occupied.
Blocking Queue--> This will invoke and tasks will be added in queue when Tasks are more than Core Pool Size can execute
Important
Why we need Thread Pool?
As the number of requests keep on increasing , we cant afford to create Thread everytime.
As it will be time taking process and overhead to create New thread Assign to task and Destroy the Thread.
So we create Thread Pool
Lets suppose max tasks we can execute is 50 and we get request of 100 Threads
So we put remaining 50 Threads in Queue
So By using Executor Service we can Directly tell Thread Pool to create Number of Threads
Sample Program
1.) We will write My Runnable
package mypackage;
public class MyRunnable implements Runnable{
public String name;
public MyRunnable(String name){
this.name=name;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Thread is :"+Thread.currentThread().getName()+"Value is +"+i);
}
}
}
package mypackage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000000; i++) {
Runnable worker = new MyRunnable("MyWorker Thread :" + i);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
VisualVM is a Tool by which you can analyze the JAVA Memory
You can find this tool in location
C:\Program Files\Java\jdk1.8.0_211\bin
Now you can see CPU Usage was high and Used Heap which is blue in color also increased
Live Threads were 110 and we gave Fixed Thread Pool size of 100
Now Lets Perform GC (Garbage Collector and see how Graph Looks)
Now You can see When GC was called , CPU Usage was increased and Heap Size was reduced a lot
Now, Take a simple example,
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(5, 10, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50));
Here, 5 is the corePoolSize - means Jvm will create new thread for new task for first 5 tasks. and other tasks will be added to the queue until queue is getting full (50 tasks).
10 is the maxPoolSize - JVM can create max 10 threads. Means if there are already 5 task/thread is running and queue is full with 50 pending tasks and if one more new request/task is arriving in queue then JVM will create new thread up to 10 (total threads=previous 5 + new 5);
new ArrayBlockingQueue(50) = is a total queue size - it can queue 50 tasks in it.
once all 10 threads are running and if new task is arriving then that new task will be rejected.
Rules for creating Threads internally by SUN:
If the number of threads is less than the corePoolSize, create a new Thread to run a new task.
If the number of threads is equal (or greater than) the corePoolSize, put the task into the queue.
If the queue is full, and the number of threads is less than the maxPoolSize, create a new thread to run tasks in.
If the queue is full, and the number of threads is greater than or equal to maxPoolSize, reject the task.
Core Pool Size--> its the core of pool which can support the number of tasks alone by creating threads
Max Pool Size--> This is the sum of threads Core pool has and number of threads it can create more if all threads of Core Pool are occupied.
Blocking Queue--> This will invoke and tasks will be added in queue when Tasks are more than Core Pool Size can execute
Important
Why we need Thread Pool?
As the number of requests keep on increasing , we cant afford to create Thread everytime.
As it will be time taking process and overhead to create New thread Assign to task and Destroy the Thread.
So we create Thread Pool
Lets suppose max tasks we can execute is 50 and we get request of 100 Threads
So we put remaining 50 Threads in Queue
So By using Executor Service we can Directly tell Thread Pool to create Number of Threads
Sample Program
1.) We will write My Runnable
package mypackage;
public class MyRunnable implements Runnable{
public String name;
public MyRunnable(String name){
this.name=name;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Thread is :"+Thread.currentThread().getName()+"Value is +"+i);
}
}
}
2.) We will write Executor Service
package mypackage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000000; i++) {
Runnable worker = new MyRunnable("MyWorker Thread :" + i);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
VisualVM is a Tool by which you can analyze the JAVA Memory
You can find this tool in location
C:\Program Files\Java\jdk1.8.0_211\bin
Now you can see CPU Usage was high and Used Heap which is blue in color also increased
Live Threads were 110 and we gave Fixed Thread Pool size of 100
Now Lets Perform GC (Garbage Collector and see how Graph Looks)
Now You can see When GC was called , CPU Usage was increased and Heap Size was reduced a lot
No comments:
Post a Comment