In concurrent programming , General use runnable, Then throw it to the thread pool and finish , In this case, the result of the thread is not required .
therefore run The return value of is void type .

If it is a multithreaded collaboration program , For example, the fibolache sequence ,1,1,2,3,5,8... Using multithreading to compute .
But the latter needs the results of the former , You need to use it callable Interface .
callable Usage and runnable equally , It's just that the call method , This method has a generic return value type , You can specify whatever you want .

Threads belong to the asynchronous computing model , So you can't get the function return value directly from other threads .

At this time ,Future And then he came out .Futrue You can monitor target thread calls call The situation , When you call Future Of get() Method to obtain the results , The current thread starts blocking , direct call Method end returns the result .

The following three simple pieces of code can be very concise to reveal this meaning :

runnable Interface implementation of concurrent programming without return value .

package com.future; import java.util.concurrent.ExecutorService; import
java.util.concurrent.Executors; public class FutureTest1 { public static class
Task implements Runnable { @Override public void run() {
System.out.println("execute!!!"); } } public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();  for (int i
= 0; i < 10; i++) { executorService.submit(new Task()); } if
(!executorService.isShutdown()) {              executorService.shutdown();   
      }  } }

callable Implementation of concurrent programming with return value .( use Future Get return value ,call Return value of String Influenced by generics )

package com.future; import java.util.ArrayList; import java.util.List; import
java.util.concurrent.Callable; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import
java.util.concurrent.Executors; import java.util.concurrent.Future; public
class FutureTest3 { public static class Task implements Callable<String> {
@Override public String call() throws Exception {
System.out.println("execute!!!"); return "complete"; } } public static void
main(String[] args) throws InterruptedException, ExecutionException {
List<Future<String>> results = new ArrayList<Future<String>>(); ExecutorService
executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++)
{ results.add(executorService.submit(new Task())); } for (Future<String> future
: results) { System.out.println(future.get()); } System.out.println("Main
complete"); if (!executorService.isShutdown()) { executorService.shutdown(); }
} }

©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions Laplance operator ( Second derivative ) Simple learning of computer composition principle pyqt Button call python program _PyQt: Link button to function in program How much can you go up once you change jobs ? Today, I saw the ceiling of job hopping python in str Function usage _python in str Usage Summary of built-in functions MySQL trigger web The server nginx---linux Installation and deployment C++ Chapter V polymorphism exercises :( It's coming to an end )python Check built-in functions , How to check python Built in function