callable and The future two feature is Java in subsequent versions in order to adapt to multi-method to join, callable is similar to runnable interface, Classes that implement callable interfaces and classes that implement runnable are tasks that can be performed by other threads. The callable interface is defined as follows; Public interface Callable<v> { V call () throws Exception; } The differences between callable and runnable are as follows: The method defined by I callable is call, and the method defined by Runnable is run. The call method of callable II can have a return value, while the runnable run method cannot have a return value. The call method of the III callable throws an exception, and the runnable run method cannot throw an exception. Future Introduction The future represents the result of an asynchronous calculation that provides a way to check whether the calculation is complete, to wait for the completion of the calculation, and to retrieve the results of the calculation. The Cancel method of the future can cancel the execution of a task, it has a Boolean parameter, the parameter is true to immediately interrupt the execution of the task, the parameter is false to allow the running task to run complete. The Get method of the future waits for the computation to complete, obtains the computation result Import java.util.concurrent.Callable; Import Java.util.concurrent.ExecutorService; Import java.util.concurrent.Executors; Import Java.util.concurrent.Future; /** * Callable and future interface * Callable is a runnable-like interface, classes that implement callable interfaces and classes that implement runnable are tasks that can be performed by other threads. * There are several differences between callable and runnable: * (1) The method prescribed by callable is call (), and the method specified by runnable is run (). * (2) callable can return a value after a task is executed, and Runnable's task cannot return a value. * (3) The call () method throws an exception, and the run () method cannot throw an exception. * (4) Run callable task can get a future object, * Future represents the result of an asynchronous calculation. It provides a way to check whether the calculation is complete, to wait for the completion of the calculation, and to retrieve the results of the calculation. * The future object can be used to understand task execution, cancel the execution of the task, and get the result of the task execution. */ public class Callableandfuture { public static class Mycallable implements callable{ private int flag = 0; Public mycallable (int flag) { This.flag = Flag; } Public String Call () throws exception{ if (This.flag = = 0) { Return "flag = 0"; } if (This.flag = = 1) { try { while (true) { System.out.println ("looping."); Thread.Sleep (2000); } } catch (Interruptedexception e) { System.out.println ("interrupted"); } Return "false"; } else { throw new Exception ("Bad flag value!"); } } } public static void Main (string[] args) { Define tasks for 3 callable types Mycallable Task1 = new mycallable (0); Mycallable task2 = new mycallable (1); Mycallable task3 = new mycallable (2); Create a service that performs a task Executorservice es = Executors.newfixedthreadpool (3); try { Submit and execute a task, and a future object is returned when the task starts. If you want the result of a task execution or an exception, you can manipulate the future object. Future Future1 = Es.submit (TASK1); Gets the result of the first task, and if the Get method is called, the current thread waits until the task finishes executing System.out.println ("Task1:" + future1.get ()); Future Future2 = Es.submit (TASK2); Wait 5 seconds before you stop the second task. Because the second task is an infinite loop. Thread.Sleep (5000); System.out.println ("Task2 Cancel:" + Future2.cancel (true)); Gets the output of the third task, because performing a third task causes an exception So the following statement will cause an exception to be thrown Future future3 = Es.submit (TASK3); System.out.println ("TASK3:" + future3.get ()); } catch (Exception e) { System.out.println (E.tostring ()); } Stop Task Execution Service Es.shutdownnow (); } } |