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(); }
} }

Technology
©2019-2020 Toolsou All rights reserved,
SQL Server Database Glossary CSS Animation effect dedecms Website is hacked How to solve hijacking to other websites Count the number of letters (java Language implementation )Java Basics ( Three ) String In depth analysis The difference between static method and non static method And storage location Django Personal blog building tutorial --- Time classified archiving Keras Save and load model (JSON+HDF5)hive Summary of processing methods for a large number of small files Website mobile phone number capture method