The Future and FutureTask

In concurrent programming, the implementation of multithreading has the inheritance of Thread and Runnable. Because of the single inheritance, we often choose Runnable instead of Thread

Similarly, neither Thread nor Runnable can obtain the execution result after the task is completed.

So Future and FutureTask become threading knowledge to master

Future

The Future can be used with a thread pool and accepts the result returned by the thread pool Submit

public class FutureLearn { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService pool= Executors.newFixedThreadPool(5); Future<String> future=pool.submit(new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(3000); return "hello"; }}); System.out.println(future.get()); pool.shutdown(); }}Copy the code

Tips:

ExecutorService pool= Executors.newFixedThreadPool(5); pool.submit(); Future<T> pool.execute(); // No return valueCopy the code

FutureTask

FutureTask creates a constructor that passes the callable function

Either exec or submit can be used in the thread pool

The other one is new Thread()start

import java.util.concurrent.*; public class MyFutureTask { public static void main(String[] args) throws ExecutionException, InterruptedException {//FutureTask is used alone //FutureTask inherits RunnableFuture, which is on the same level as Runable and has the run method. FutureTask<String> FutureTask =new FutureTask<>(new) Callable<String>() { @Override public String call() throws Exception { Thread.sleep(2000); return "hello,world.I am FutureTask"; }}); new Thread(futureTask).start(); / / / / FutureTask can also cooperate with the thread pool in using the ExecutorService pool = Executors. NewFixedThreadPool (2); FutureTask<String> futureTask1=new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception  { Thread.sleep(1000); Return "Use FutureTask in thread pool "; }}); pool.execute(futureTask1); System.out.println(futureTask1.get()); System.out.println(futureTask.get()); pool.shutdown(); }}Copy the code

Demo

code

package com.company; import java.util.concurrent.*; public class Call { public static void main(String[] args) throws ExecutionException, InterruptedException { Callable<String> callable=new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(6000); return "---------------------1"; }}; FutureTask<String> future=new FutureTask<>(callable); new Thread(future).start(); new Thread(new Runnable() { @Override public void run() { try { System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }).start(); ExecutorService executorService=Executors.newFixedThreadPool(5); for (int i = 0; i < 5; i=i+1) { int finalI = i; Future future1=executorService.submit(new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(finalI*1000); return "==========================number is "+ finalI; }}); new Thread(new Runnable() { @Override public void run() { try { System.out.println(future1.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }).start(); } for (int i = 0; i < 10; i++) { Thread.sleep(1000); System.out.println("main sleep "+i); } executorService.shutdown(); }}Copy the code

output

==========================number is 0
main sleep 0
==========================number is 1
==========================number is 2
main sleep 1
==========================number is 3
main sleep 2
==========================number is 4
main sleep 3
main sleep 4
---------------------1
main sleep 5
main sleep 6
main sleep 7
main sleep 8
main sleep 9

Process finished with exit code 0
Copy the code