Introduction to Java Future mode
The future mode is a common pattern for Java multithreading, and the JDK also has built-in support for future patterns, such as the Futuretask class under the Java.util.concurrent package. The core idea is that the object can be returned immediately after the request is made, but the object is actually a dummy object and cannot be used immediately, but we can use the dummy object to get the result after doing something else. What is this for? Because when a dummy object is returned, a thread is secretly opened to request the actual result data. So below, let's simulate the implementation of the next future model.
Code implementation and Analysis
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/8B/06/wKiom1hBiN2QQwJyAABNwUkohec540.png "title=" Sogou _ 2016-12-02_22-45-02.png "alt=" Wkiom1hbin2qqwjyaabnwukohec540.png "/>
The main process is: The client class makes a request to return, doing some other work after obtaining the requested result data.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/8B/03/wKioL1hBiSvTqru3AABB2oQ8_tI599.png "title=" Sogou _ 2016-12-02_22-46-21.png "alt=" Wkiol1hbisvtqru3aabb2oq8_ti599.png "/>
Through, it has been clearly seen that when the data is fetched, it returns immediately, and a thread is opened at the same time.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/8B/03/wKioL1hBiauQnAdEAABZpIHVeL4815.png "title=" Sogou _ 2016-12-02_22-48-30.png "alt=" Wkiol1hbiauqnadeaabzpihvel4815.png "/>
What you need to pay special attention to here is the use of wait/notify, so why use them?
Now that we return a "dummy" after the request, what if we call the "dummy" method to get the result data immediately? We want the "dummy" to call the method to return the result data after the thread task has been secretly opened. Therefore, using wait to block the result of "dummy" first returns, when the thread completes the result data request after the Notify notification "dummy" can provide the result data.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8B/06/wKiom1hBi1iCZ4xVAABR4JPR7X0939.png "title=" Sogou _ 2016-12-02_22-55-42.png "alt=" Wkiom1hbi1icz4xvaabr4jpr7x0939.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/8B/03/wKioL1hBi33ht9oTAAAOkmcFWLg764.png "title=" Sogou _ 2016-12-02_22-56-17.png "alt=" Wkiol1hbi33ht9otaaaokmcfwlg764.png "/>
Run result validation:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/8B/06/wKiom1hBi62zaD2EAAAn2fCy8DQ577.png "title=" Sogou _ 2016-12-02_22-57-06.png "alt=" Wkiom1hbi62zad2eaaan2fcy8dq577.png "/>
This article is from the "Boundless Mind Infinite" blog, please be sure to keep this source http://zhangfengzhe.blog.51cto.com/8855103/1879036
Java Future Mode implementation