ThreadPoolExecutor pool = new ThreadPoolExecutor (1 // core number of threads
, the maximum number of threads @ 3
, 60 @ thread survival time
, TimeUnit.SECONDS,
new new ArrayBlockingQueue with <> (2) // bounded queue
, a ThreadFactory new new () {
@ override
public newthread the Thread (the Runnable R & lt) {
the Thread new new TH = the Thread (R & lt, "my_thread");
IF (! th.getPriority () = Thread.NORM_PRIORITY) {
th.setPriority (Thread.NORM_PRIORITY);
}
IF (TH. isDaemon ()) {
th.setDaemon (to false);
}
Return TH;
}
}, new new RejectedExecutionHandler () {
@Override
public void rejectedExecution (R & lt the Runnable, the ThreadPoolExecutor Executor) {
System.err.println ( "current task has been rejected:" + R & lt);
}
});
Task t1 = new Task(1,1);
Task t2 = new Task(2,2);
Task t3 = new Task(3,3);
Task t4 = new Task(4,4);
Task t5 = new Task(5,5);
Task t6 = new Task(6,6);
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
pool.execute(t6);
pool.shutdown();
public class Task implements Runnable{
private int id;
private int count;
public Task(int id, int count) {
super();
this.id = id;
this.count = count;
}
@Override
public void RUN () {
System.err.println ( "Thread Processing Task:" + this.id);
the try {
the Thread.sleep (3000);
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
@Override
public String toString() {
return "Task [id=" + id + ", count=" + count + "]";
}
}
From the point of view of operating results Code
Task processing threads: a
processing thread tasks: 5
processing threads tasks: 4
current task has been rejected: Task [id = 6, count = 6]
processing threads Task: 2
processing threads Task: 3
When the execution thread t1 directly perform tasks using the core thread initialization
When the execution thread t2, in which case the number of threads is greater than the core threads, the threads are bounded speaking t2 queue
When a thread of execution time t3, when the number of threads is greater than the core threads, the threads are bounded t3 queue
When the thread of execution t4, in which case the queue is full, the number of threads at this time is not to exceed the maximum thread, a new thread is enabled to perform tasks
When the execution thread t5, in which case the queue is full, the number of threads at this time is not to exceed the maximum thread, a new thread is enabled to perform tasks
When the thread of execution t6, in which case the queue is full, at this time exceeds the maximum number of threads the thread, enable their rejection policy
Note that, if the ArrayBlockingQueue bounded queue, the queue LinkedBlockingQueue is replaced by unbounded thread pool works
When the number of threads does not exceed the number of kernel threads, to enable the thread to perform the task, when the number of threads is greater than the number of kernel threads, it is added directly to the unbounded queue waiting to be executed, the number of maximum thread after you set is in fact no role of