One order

  conventional thread inside , Unable to get execution results after task execution . Get results by sharing variables or using thread communication , It's troublesome .

namely Runnable Interface , The real business of thread is run inside .

jdk1.5 Start offering Callable and Future, Through them, we can get the result of task execution after the task is executed .

Two future
   Callable and Future be located java.util.concurrent Package .   Callable It's an interface , There is only one way call(). Usually with
ExecutorService use .   
Future For specific Runnable perhaps Callable The execution result of the task is cancelled , Is the query complete , Get the results . If necessary, it can be passed through get Method to get the execution result , This method blocks until the task returns the result . See api. It can be summarized as follows :
1) Judge whether the task is completed ;2) Ability to interrupt tasks ;3) Be able to obtain task execution results .
demo: package com.daojia.future; import java.util.Random; import
java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import
java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; /** * * @author daojia * */
public class FutureTest { public static void main(String[] args) throws
InterruptedException, ExecutionException, TimeoutException { // TODO
Auto-generated method stub useFuture(); } private static void useFuture()
throws InterruptedException, ExecutionException, TimeoutException {
ExecutorService exector = Executors.newFixedThreadPool(3); Future<Integer>
futureA = (Future<Integer>) exector.submit(new Task("A1")); Future<Integer>
futureB = (Future<Integer>) exector.submit(new Task("B1"));
System.out.println("a,b Request completed "); int a= futureA.get(1, TimeUnit.SECONDS); int b =
futureB.get(1, TimeUnit.SECONDS); exector.shutdown();
System.out.println(" All tasks are completed ,res="+(a+b)); } }class Task implements
Callable<Integer>{ private String name; public Task(String name){ this.name =
name; } @Override public Integer call() throws Exception {
System.out.println(System.currentTimeMillis()+" "+name+"run"); Random random =
new Random(); int num = random.nextInt(100); Thread.sleep(num);
System.out.println(System.currentTimeMillis()+" "+name+"runover,res="+num);
return num; } } Simulate a time-consuming task, The main thread is responsible for summarizing the results . give the result as follows a,b Request completed 1508849841494 A1run
1508849841495 B1run 1508849841503 B1runover,res=8 1508849841582
A1runover,res=87 All tasks are completed ,res=95

Three JDK8 CompletableFuture
  jdk8CompletableFuture New features are also available , Multiple asynchronous processors can be choreographed
demo: public class FutureTest { public static void main(String[] args) throws
InterruptedException, ExecutionException, TimeoutException { // TODO
Auto-generated method stub useCompletableFuture(); } private static void
useCompletableFuture() throws InterruptedException, ExecutionException {
System.out.println("CompletableFuture"); CompletableFuture<Void> futureA =
CompletableFuture.runAsync(() -> Task("A2")); CompletableFuture<Void> futureB =
CompletableFuture.runAsync(() -> Task("B2")); futureA.runAfterEither(futureB,
() ->Task("C2")).get(); } private static Object Task(String name) {
System.out.println(name + " starts at " + LocalTime.now()); try { Random random
= new Random(); TimeUnit.SECONDS.sleep(random.nextInt(10)); } catch
(InterruptedException e) { } System.out.println(name + " ends at " +
LocalTime.now()); return null; } }
function :
CompletableFuture A2 starts at 21:19:43.562 B2 starts at 21:19:43.562 A2 ends
at 21:19:45.563 C2 starts at 21:19:45.563 B2 ends at 21:19:46.563 C2 ends at
21:19:54.563
so , stay A perhaps B Deal with it first ,C Just start again . Add a copy jdk8 Related class diagram
 
This needs further study .API Many different approaches support flexible choreography .

Technology
©2019-2020 Toolsou All rights reserved,
Error summary -myBatis plus paging use easyPOI Import Excel data In the problem of target detection “ recall Recall”,“ Accuracy Precision”vue use vue-clipboard2 Realize the function of copy link C In language switch sentence Wechat applet (uni-app)url Parameter transfer object hive compress &&hdfs Merge small files hive Summary of processing methods for a large number of small files use spring Of AntPathMatcher matching url route Linux Page replacement algorithm C Language implementation