import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler
{
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor)
{
System.out.println(r.toString() + " is rejected");
}
}
MonitorThread.java
import java.util.concurrent.ThreadPoolExecutor;
public class MonitorThread implements Runnable
{
private ThreadPoolExecutor executor;
private int seconds;
private boolean run = true;
public MonitorThread(ThreadPoolExecutor executor, int delay)
{
this.executor = executor;
this.seconds = delay;
}
public void shutdown()
{
this.run = false;
}
@Override
public void run()
{
while (run)
{
System.out.println(String.format(
"[monitor] [%d/%d] Active: %d, Completed: %d, "
+ "Task: %d, isShutdown: %s, isTerminated: %s",
this.executor.getPoolSize(),
this.executor.getCorePoolSize(),
this.executor.getActiveCount(),
this.executor.getCompletedTaskCount(),
this.executor.getTaskCount(), this.executor.isShutdown(),
this.executor.isTerminated()));
try
{
Thread.sleep(seconds * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
RunnableTask.java
public class RunnableTask implements Runnable
{
private String taskNumber;
public RunnableTask(String taskNumber)
{
this.taskNumber = taskNumber;
}
@Override
public void run()
{
System.out.println(Thread.currentThread().getName()
+ " Start. taskNumber = " + taskNumber);
processCommand();
System.out.println(Thread.currentThread().getName()
+ " End of taskNumber =" + taskNumber);
}
private void processCommand()
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
@Override
public String toString()
{
return this.taskNumber;
}
}
ThreadPool.java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPool
{
public static void main(String[] args) throws InterruptedException
{
// RejectedExecutionHandler implementation
RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();
// Get the ThreadFactory implementation to use
ThreadFactory threadFactory = Executors.defaultThreadFactory();
// creating the ThreadPoolExecutor
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2),
threadFactory, rejectionHandler);
// start the monitoring thread
MonitorThread monitor = new MonitorThread(executorPool, 3);
Thread monitorThread = new Thread(monitor);
monitorThread.start();
// submit work to the thread pool
for (int i = 1; i <= 10; i++)
{
executorPool.execute(new RunnableTask("task" + i));
}
Thread.sleep(30000);
// shut down the pool
executorPool.shutdown();
// shut down the monitor thread
Thread.sleep(5000);
monitor.shutdown();
}
}
Output
pool-1-thread-1 Start. taskNumber = task1
pool-1-thread-2 Start. taskNumber = task2
pool-1-thread-3 Start. taskNumber = task5
task7 is rejected
task8 is rejected
pool-1-thread-4 Start. taskNumber = task6
task9 is rejected
task10 is rejected
[monitor] [0/2] Active: 0, Completed: 0, Task: 5, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End of taskNumber =task1
pool-1-thread-1 Start. taskNumber = task3
pool-1-thread-2 End of taskNumber =task2
pool-1-thread-2 Start. taskNumber = task4
pool-1-thread-3 End of taskNumber =task5
pool-1-thread-4 End of taskNumber =task6
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End of taskNumber =task3
pool-1-thread-2 End of taskNumber =task4
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
Refer:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html
Click the below link to download the code: