After Java5, the concurrent Threads section adds a lot of new things, a new start, dispatch, and management thread of a lot of API, at this time through executor to start the thread than Thread.Start () better, easier to control thread startup, destruction, etc., can also use the function of the thread pool.
I. Creating a Task
is actually implementing the Runnable interface to implement the Run method.
Two. Perform Tasks
The task is performed through the Java.util.concurrent.ExecutorService interface object, which is created through a static method of the tool class java.util.concurrent.Executors.
Executors factory and utility methods for the Executor, Executorservice, Scheduledexecutorservice, Threadfactory, and callable classes defined in this package.
1. Create thread pool (for Scheduledexecutorservice)
private static Scheduledexecutorservice Singlescheduler = Executors.newscheduledthreadpool (4);
2. Adding tasks to the thread pool
When a task is added to the thread pool, the thread pooling assigns each task a task to run. The execution time can be timed and can be temporary.
(1) Perform temporary tasks
Scheduledexecutorservice has two better ways to perform a temporary task execute (), submit ()
Three differences:
A. Different parameters
Execute () has only one method to receive the Runnable class,
Submit () has three overloaded methods, receiving the Runnable class, callable class, and runnable, respectively, T (the value to perform a successful return)
B. return value
Execute () has no return value, and submit () has an identity that returns whether a future<> is successful after execution. If a task executes, you need to know if the execution succeeds, and if it fails, what the cause is.
C.submit Convenient exception handling
(2) Perform timed tasks Scheduleatfixedrate,schedulewithfixeddelay
A.scheduleatfixedrate
Specific parameter description
Public scheduledfuture<?> scheduleatfixedrate (Runnable command, long initialdelay,Long Period, timeunit unit);
command execution of the thread; InitialDelay initialization delay; period; two starts execution minimum time interval; Unit timing units
B.schedulewithfixeddelay
Specific parameter description
Public scheduledfuture<?> schedulewithfixeddelay (Runnable command, long initialdelay,long delay, Timeunit unit);
command execution thread; initialdelay initialization delay; interval (interval execution delay) at the end of the previous execution to the start of the next execution; Unit time units
Third, close the object
Shutdown ();
Iv. examples
1. Simple Runnable class
Excute () and submit () the execution of a single parameter is relatively simple and not listed.
Package test; Public class Implements runnable{ public Threaddemo () { } @Override public void Run () { System.out.println ("simple execution!") ); }}
2. Execution of temporary tasks
Packagetest;Importjava.util.concurrent.ExecutionException;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future;ImportJava.util.concurrent.ScheduledExecutorService; Public classTestthread {Private StaticScheduledexecutorservice Singlescheduler = Executors.newscheduledthreadpool (4); Public Static voidMain (string[] args) { future<String> F =singlescheduler.submit (NewThreaddemo (), "Success!"); Try{System.out.println (F.get ()); } Catch(interruptedexception e) {e.printstacktrace (); } Catch(executionexception e) {e.printstacktrace (); } }}
return results
Simple Execution! Success !
3. Execution of timed tasks
Schedulewithfixeddelay differs from this method only with some differences in parameters.
Packagetest;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.ScheduledExecutorService;ImportJava.util.concurrent.TimeUnit; Public classTestthread {Private StaticScheduledexecutorservice Singlescheduler = Executors.newscheduledthreadpool (4); Public Static voidMain (string[] args) {singlescheduler.scheduleatfixedrate (NewThreaddemo (), 1, 1, Timeunit.seconds); }}
return results
Simple Execution! Simple Execution ! Simple Execution ! Simple Execution ! Simple Execution !
Scheduledexecutorservice timed tasks, threads