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,
Java Swing JList: List box components Accurate mobile phone number capture , Big data of operators The difference between memory overflow and memory leak , Causes and Solutions Website mobile phone number capture method stay Vue Use in Web Worker3 species Python data structure ,13 Creation methods , This is the conclusion , Great ! What's the difference in server prices ? Basic definition and use of function ‘ To be continued SQL Server Database Glossary Chrome OS, For programmers and Windows What does it mean ? Internet Marketing