When using a thread to handle multiple short tasks in the Java program, use a technique called Thread Pooling is a wise choice. In order not to create a single task (and end at the end of the task) thread, we often write a thread pool that is suitable for different operating environment. Although the specific attribute parameters of these thread pools are not the same, they are roughly pursued as the following applications:
Turn the task group into the thread pool,
Implement an execution condition if a thread in the thread pool meets
After the task is executed, the thread returns the thread pool.
Do not meet the implementation conditions (such as thread pool size conditions), wait for the execution
J2se5.0 now provides a new Java.util.concurrent package, which has several frameworks for constructing a good thread pool.
For example: The Executor interface provides a separate approach Execute to receive an instance object of Runnable:
Public interface executor {
Public void Execute (Runnable Command);
}
So we can create an Executor object and introduce the runnable task:
Executor Executor = ...; // Can be created after IMPLEMENTS ...
Executor.execute (arunnable1);
Executor.execute (arunnable2);
E.g:
Class myexecutor implements executor {
Public void execute (Runnable R) {
New Thread (r) .start ();
}
}
In ConcURRENCY, a ThreadPoolexecutor class also provides a thread pool operation for general general use. The following is a constructor of such a class. We can determine some attributes of such a class such as: pool size, activity time, thread factory and Refuse to run the thread controller.
Public Threadpoolexecutor (int Corepoolsize,
Int MaximumPoolsize,
Long KeePaliveTime,
TimeUnit Unit,
BlockingQueue
Workqueue)
Public Threadpoolexecutor (int Corepoolsize,
Int MaximumPoolsize,
Long KeePaliveTime,
TimeUnit Unit,
BlockingQueue
WorkQueue,
ThreadFactory ThreadFactory)
Public Threadpoolexecutor (int Corepoolsize,
Int MaximumPoolsize,
Long KeePaliveTime,
TimeUnit Unit,
BlockingQueue
WorkQueue,
RejectedexecutionHandler Handler)
Public Threadpoolexecutor (int Corepoolsize,
Int MaximumPoolsize,
Long KeePaliveTime,
TimeUnit Unit,
BlockingQueue
WorkQueue,
ThreadFactory ThreadFactory,
RejectedexecutionHandler Handler)
In fact, we don't need to use the constructor to create a default thread pool in the EXECUTORS class. For example, we can create the NewfixedThreadPool method in Executors. For example, we can call NewfixedThreadPool Measures in Executors to regulate the thread pool you need. Size. You can use a EXECTUORSERVICE class that inherits the Executor to run or submit the Runnalbe task group. Submit measures for ExectuorService - Submit allows a result F, and returns Future Object, which can be used to check if the task has been executed. Below is a Specific application example: - From Sun Technology Forum
Public class nameprinter imports runnable {
PRIVATE FINAL STRING NAME;
PRIVATE FINAL INT DELAY
Public nameprinter (string name, int delay) {
THIS.NAME = Name;
THIS.DELAY = delay;
}
Public void run () {
System.out.println ("Starting:" Name);
Try {
Thread.sleep (delay);
} catch (interruptedException ignored) {
}
System.out.println ("Done with:" Name);
}
}
-------------------------------------------------- --------------------------------
Import java.util.concurrent. *;
Import java.util.random;
Public class usepool {
Public static void main (string args []) {
Random Random = new random ();
ExecutorService Executor =
Executors.newfixedthreadPool (3); // Setting the thread pool capacity 3
// sum Up Wait Times To Know When to Shutdown
INT waittime = 500;
For (int i = 0; i <10; i ) {
String name = "nameprinter" i;
INT TIME = Random.nextint (1000);
Waittime = Time;
Runnable Runner = New Nameprinter (Name, TIME);
System.out.println ("Adding:" Name "/" TIME);
Executor.execute (Runner);
}
Try {
Thread.sleep (waittime);
EXECUTOR.SHUTDOWN ();
Executor.awaitterMination
Waittime, TimeUnit.milliseConds;
} catch (interruptedException ignored) {
}
System.exit (0);
}
}
-------------------------------------------------- --------------------------------
May output:
Unique with the Random Sleeps Present:
Adding: Nameprinter 0 / 30Adding: Nameprinter 1/727
Adding: Nameprinter 2/980 // The top 3 adds is obviously faster
Starting: Nameprinter 0
Starting: Nameprinter 1
Starting: Nameprinter 2
Add: Nameprinter 3/409
Adding: Nameprinter 4/49
Adding: NamePrinter 5/802
Adding: NamePrinter 6/211
Adding: Nameprinter 7/459
Adding: Nameprinter 8/994
Adding: NamePrinter 9/459
Done with: NamePrinter 0 Although all tasks have been added to the thread pool,
Starting: Nameprinter 3 but because thread pool capacity is 3,
Done with: Nameprinter 3 The first three tasks are being executed,
Starting: Nameprinter 4 So the task 3 has been waiting until the end of the task 0
Done with: Nameprinter 4
Starting: Nameprinter 5
Done with: Nameprinter 1
Starting: Nameprinter 6
Done with: Nameprinter 6
Starting: Nameprinter 7
Done with: Nameprinter 2
Starting: Nameprinter 8
Done with: Nameprinter 5
Starting: Nameprinter 9
Done with: Nameprinter 7
Done with: Nameprinter 9
Done with: Nameprinter 8
There are still many other content in the thread pool frame, please refer to the API.