Today, we go on to the article to continue our schedule. The first two examples we call the method is not return is worthwhile, but the actual application, the general method is to have the return is worth, we this time to modify, to see how the return is worth the method of scheduling. one, always scheduling deformation 1, control class
public class Timetest {
/**
* Time Schedule
* 3 seconds to start, print every 1 seconds *
/public
static String Executeperday () {
/ / Create a thread pool that reuses the number of fixed threads to run them in a shared, unbounded queue.
Scheduledexecutorservice executor = executors.newscheduledthreadpool (1);
System.out.println ("Always scheduling");
System.out.println ("Gettest ()" + New Date (). toString ());
Future Future =executor.schedule (
new Returntest (),// call execution Method Class
3000, //delay 3 seconds after execution
Timeunit.milliseconds);
String result= "";
try {
//Fetch returns result
= Future.get (). toString ();
\ catch (interruptedexception e) {
//TODO Auto-generated Catch block
e.printstacktrace ();
} catch (Executionexception e) {
//TODO auto-generated Catch block
e.printstacktrace ();
}
return result;
}
2. True performer
public class Returntest implements callable<string> {
@Override public
String call () throws Exception {
//invoke the method that is actually executed return
getnowtime ();
}
Private String Getnowtime () {
//Print test time
string result= "Gettest ()" + New Date (). toString ();
SYSTEM.OUT.PRINTLN (result);
return result;
}
3. Main class
public class Maintest {public
static void Main (string[] args) {
timetest test = new Timetest ();
Dispatch
String Result=test.executeperday () at all times;
System.out.println ("Received--" +result);
}
Second, analysis 1, executors (thread pool)
Starting with JAVA5, a new Executors tool class is created to generate a thread pool, which has several static factory methods to create it.
public static Executorservice newfixedthreadpool (int nthreads)
Create a thread pool that reuses the number of fixed threads to run them in a shared, unbounded queue.
At any point, most nthreads threads are in the active state of the processing task. If additional tasks are committed when all threads are active,
The additional task will wait in the queue until the thread is available. If any thread terminates due to failure during execution prior to shutdown, a new thread will perform the subsequent task (if needed) instead. The threads in the pool will persist until a thread is explicitly closed.
public static Executorservice Newsinglethreadexecutor ()
Create a Executor that uses a single worker thread to run the thread in a unbounded queue. (Note that if this individual thread is terminated because of a failure during execution prior to shutdown, a new thread will perform the successor task if needed).
Each task is guaranteed to be performed sequentially, and no multiple threads are active at any given time. Unlike other equivalent Newfixedthreadpool (1), it is guaranteed that threadpoolexecutor can not be reconfigured to use more threads.
Note: The difference between Newsinglethreadexecutor and Newfixedthreadpool (1) is:
The Excutorservice returned by Newsinglethreadexecutor in the destructor Finalize () invokes shutdown (), that is, if we do not call shutdown () on it, Then you can make sure that it calls shutdown () to terminate the thread when it is reclaimed. 2, Scheduledexecutorservice (Task scheduling)
On the basis of Executorservice, Scheduledexecutorservice provides the ability to perform tasks on a schedule, which provides the following main methods:
Schedule (Task,initdelay): Schedule the submitted callable or runnable tasks to be performed after the specified time in Initdelay. Can be used to delay the execution of a task with a return value
Scheduleatfixedrate (): Schedules the submitted runnable tasks to be repeated at specified intervals. Can be used to perform recurring tasks
Schedulewithfixeddelay (): Schedule the submitted runnable task to repeat after each execution, waiting for the time specified by delay. Can be used to perform periodic timed tasks
3, Future (Accept return parameters)
Future represents the result of an asynchronous computation. It provides a way to check whether the calculation is complete, to wait for the calculation to complete, and to get the result of the calculation.
You can only use the Get method to obtain the results after the calculation is complete, and if necessary, block this method before the calculation completes. Cancellation is performed by the Cancel method. Other methods are provided to determine whether the task was completed normally or was canceled. Once the calculation is complete, the calculation can no longer be canceled. Summary:
With regard to time scheduling, we can now call arbitrary methods at times or at regular intervals, but there is a problem, that is, when the call has a return-worthy method, we can only delay the call, but not the periodic call. For this, we will be in the future learning process, the use of other ways to solve.