Future Mode description

* Traditional single thread environment , The calling function is synchronous , You must wait for the program to return the results , Other treatment can be carried out .
Futrue Mode , Call mode changed to asynchronous .
* Futrue The core of the model is : Making full use of waiting time in principal function , Use waiting time to process other tasks , Make full use of computer resources .
Main roles

* Main: System startup , call Client Make a request
* Client: return Data object , Return immediately FutureData, And open ClientThread Thread assembly RealData
* Data: Interface to return data
* FutureData:Futrue data , It's a virtual data , Assembly required RealData
* RealData: Real data , The structure is relatively slow .
JDK Built in implementation
——-jdk Built in future Implementation of pattern . The most important thing module is FutrueTask class , It's done Runnable Interface , Run as a separate thread , In its run()
Method , adopt Sync Inner class , call Callable Interface , And maintenance Callable The return object of the interface . use FutureTask.get() method , return Callable The return object of the interface .
The specific use is as follows :

package com.y.www; import java.util.concurrent.Callable; /** * @description: *
@author: lbYue * @date: Created in 14:11 2018/1/9 */ public class RealData
implements Callable<String> { private String para; public RealData(String
para){ this.para = para; } @Overridepublic String call() throws Exception {
// Real business logic StringBuffer sb = new StringBuffer(); for (int i= 0 ;i < 10 ; i++){
sb.append(para);try { Thread.sleep(100); }catch (InterruptedException e){ } }
return sb.toString(); } }
package com.y.www; import java.util.concurrent.*; /** * @description: * @author
: lbYue * @date: Created in 14:15 2018/1/9 */ public class MainTest { public
static void main(String[] args) throws InterruptedException,ExecutionException{
FutureTask<String> future =new FutureTask<String>(new RealData("a"));
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(future); System.out.println(" Request completed !"); try { System.out.println(
" I'm sleeping. Don't disturb me !2 Response in seconds "); Thread.sleep(2000); }catch (InterruptedException e){ }
System.out.println(" Data processing completed :"+future.get()); } }
Console output results

©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 )