It turns out that tiger already comes with this thread pool, that is, when the number of tasks exceeds the limit, the thread that can block the tasks to be placed is mainly used for JMS message listening.
Start:
In threadpoolexcecutor, new arrayblockingqueue (max_task) is input. if the task is exceeded, the error "rejectedexecutionexception" is returned.
Later, you don't need to execute the method to add a task. Instead, you can directly getqueue (). Add (task) and use its blocking feature. However, blocking is easy to use, but the task is not processed. After reading the queue, I was dizzy. It was originally in it and the task pool did not process it. The task pool is required.
Finally, we reload a blockedthreadpoolexecutor:
Private Reentrantlock pauselock = New Reentrantlock ();
Private Condition unpaused = Pauselock. newcondition ();
@ Override
Public Void Execute (runnable command) {
Pauselock. Lock ();
Try {
While(Getpoolsize ()=Getmaximumpoolsize ()&&Getqueue (). remainingcapacity ()=0)
Unpaused. Await ();
Super. Execute (command );//Put it out of the lock, there will be leakage in the stress test!
} Catch (Interruptedexception E) {
Log. Warn (This, E );
} Finally {
Pauselock. Unlock ();
}
}
@ Override
Protected Void Afterexecute (runnable R, throwable T) {
Super . Afterexecute (R, t );
Try {
Pauselock. Lock ();
Unpaused. Signal ();
} Finally {
Pauselock. Unlock ();
}
}
MultithreadingProgramIt is easy to make mistakes. If you have completed the writing, you should try your best to use stress testing. Otherwise, there will be a lot of problems ~~~