One .Future Class diagram structure of

       
Future The interface defines the main 5 Interface methods , Yes RunnableFuture and SchedualFuture Inherit this interface , as well as CompleteFuture and ForkJoinTask Inherit this interface .

RunnableFuture

         This interface inherits at the same time Future Interface and Runnable Interface , On successful execution run() After the method , It can be done through Future Access execution results . This interface implements the class FutureTask, A cancelable asynchronous computation , This class provides Future The basic implementation of , Behind us demo It is also implemented with this class , It can start and cancel a calculation , Query whether the calculation has been completed , Restore calculation results . The results of the calculation can only be restored if the calculation has been completed . If the calculation is not completed ,get Methods can block , Once the calculation is complete , This calculation cannot be restarted or cancelled , Unless you call runAndReset method .

        FutureTask Can be used to package one Callable or Runnable object , Because it did Runnable Interface , And it can be delivered to Executor Carry out . To provide a singleton class , This class provides the protected Constructor .

SchedualFuture

         This interface indicates that a delayed behavior can be cancelled . Usually one is arranged future It's a timed mission SchedualedExecutorService The results of

CompleteFuture

         One Future Class is the completion of the display , And can be used as a completion level , Trigger supported dependent functions and behaviors through its completion . When two or more threads are about to perform a complete or cancel operation , Only one can succeed .

ForkJoinTask

         Task based abstract class , It can be done through ForkJoinPool To execute . One ForkJoinTask Is similar to a thread entity , But it's lightweight relative to thread entities . A large number of tasks and subtasks are ForkJoinPool The real thread in the pool hangs , At the expense of some usage restrictions .

 

Two . Future Main methods

Future The interface mainly includes 5 Methods

get() Method returns a result when the task is finished , If the , The work is not over yet , The thread is blocked , Until the mission is completed

get(long timeout,TimeUnit unit) Wait longer timeout The result will be returned

cancel(boolean
mayInterruptIfRunning) Method can be used to stop a task , If the task can be stopped ( adopt mayInterruptIfRunning To judge ), You can return to true, If the task has been completed or stopped , Or the task can't be stopped , Will return false.

isDone() Method to determine whether the current method is completed

isCancel() Method to determine whether the current method is canceled

 

3. Future contrast demo

NormalThread:
public class NormalThreadTest { public static void main(String[] args) throws
Exception { long start = System.currentTimeMillis(); // Start purchasing kitchen utensils thread ShoppingThread
shopping = new ShoppingThread(); shopping.start(); shopping.join(); //
Ensure the purchase and delivery of kitchenware // Get to buy Kitchenware KitchenWare kc = shopping.kc; // Buying ingredients FoodMaterial fm = new
FoodMaterial(); Thread.sleep(2000); System.out.println(" Step two : The ingredients are in place "); // Cooking food
cooking(kc, fm); System.out.println(" Step three : Gourmet cooking finished "); long end =
System.currentTimeMillis(); System.out.println(" Cooking time is :" + (end - start)); }
/** * Define online shopping kitchenware thread * @author Administrator * */ static class ShoppingThread
extends Thread { // Kitchen object reference private KitchenWare kc; @Override public void run()
{ System.out.println(" The first step : Order online "); System.out.println(" The first step : Waiting for kitchenware "); try {
Thread.sleep(5000); // Waiting for kitchen utensils } catch (InterruptedException e) {
e.printStackTrace(); } System.out.println(" The first step : Express delivery "); // Production of kitchenware kc = new
KitchenWare(); } } /** * Kitchenware * @author Administrator * */ static class
KitchenWare { } /** * Food materials * @author Administrator * */ static class
FoodMaterial { } /** * Defining how to cook food * @param kc * @param fm */ static void
cooking(KitchenWare kc, FoodMaterial fm) { } }
FutureThread:
public class FutureThreadTest { public static void main(String[] args) throws
Exception { long start = System.currentTimeMillis(); Callable<KitchenWare>
callable = new Callable<KitchenWare>() { public KitchenWare call() throws
Exception { System.out.println(" The first step : Order online "); System.out.println(" The first step : Waiting for kitchenware ");
try { Thread.sleep(5000); // Waiting for kitchen utensils } catch (InterruptedException e) {
e.printStackTrace(); } System.out.println(" The first step : Express delivery "); return new
KitchenWare(); } }; // Objects wrapped for asynchronous execution FutureTask<KitchenWare> task = new
FutureTask<>(callable); new Thread(task).start(); // Buying ingredients FoodMaterial fm = new
FoodMaterial(); Thread.sleep(2000); System.out.println(" Step two : The ingredients are in place "); if
(!task.isDone()) { System.out.println(" The kitchenware hasn't arrived yet ....."); } // The result of asynchronous block execution is obtained by blocking
KitchenWare kc = task.get(); // block // Cooking food cooking(kc, fm);
System.out.println(" Step three : Gourmet cooking finished "); long end = System.currentTimeMillis();
System.out.println(" Cooking time is :" + (end - start)); } /** * Kitchenware * @author
Administrator * */ static class KitchenWare { } /** * Food materials * @author
Administrator * */ static class FoodMaterial { } /** * Defining how to cook food * @param kc *
@param fm */ static void cooking(KitchenWare kc, FoodMaterial fm) { } }
 

Technology
©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )