Future The pattern is similar to js Medium ajax etc. , Is an asynchronous data acquisition mechanism , Here I understand some of my own image through the code to achieve .
This mechanism can be interpreted as : Call the method to get the data , First get an empty bin without data ( This box has a mechanism for getting data and loading data ), As for the data in the box, it is obtained by opening another thread , After a while , When we want to get the data in the box , Just take it from the box , Normally , It should have been some time since I got the box before I needed to get the data from it ( Because there are other operations ), It was this period of time , The data has been stored in the bin through other threads .
/** * test * @author yangcheng * */ public class MainTest { public static void
main(String[] args){ Client client=new Client(); // Get the empty boxes FeatureCar
result=(FeatureCar) client.requestData(" This is the data I sent ");
System.out.println(" Carry on other business "); // Get data from empty boxes If there is no data, wait , Wait until you get the data location ———— therefore
Some other operations can be done before getting the data in the bin System.out.println(result.getResult()); } }
/** * In the client , Use approximate to javascript Medium ajax The asynchronous request of data in this way to get data * * The role of the client
The structure of asynchronous data acquisition needs to be built , In order to let main Method directly uses the class object to get its data * * @author yangcheng * */ public class
Client { // public Data requestData(final String msg){ // Create a synchronization object and return directly ( A box containing articles )
final FeatureCar car=new FeatureCar(); // Create a thread
Used to get data ( Get items in the box , This item is after the box has been handed over to the requester , Slowly put it in the box ) new Thread(new Runnable() {
@Override public void run() { // TODO Auto-generated method stub
FeatureRealData realData=new FeatureRealData(msg); // Put the real data in the box
car.setRealData(realData); } }).start();; // Return the box to the caller first And the data inside is put in asynchronously through the above thread
return car; } }
/** * case * The requested data is used to synchronize the client's data ( Stuff “ case ”, The box was empty when it was just returned to the caller , The data inside is obtained through asynchronous threads ) * * @author
yangcheng * */ public class FeatureCar implements Data{ // Combining a real data object private
FeatureRealData realData;
// Here, we need to set an identifier for judgment realData In the object null, If null When getResult Method is executed , You need to getResult block (wait())
// There is no value by default private boolean flag=false; public synchronized void
setRealData(FeatureRealData realData) { if(flag){ try {
// If the synchronized Method invocation method wait notify notifyAll
Bulletin "java.lang.IllegalMonitorStateException" wait(); } catch
(InterruptedException e) { e.printStackTrace(); } } // default setRealData Methods can be executed directly
It won't block , flag=true; notify(); this.realData = realData; } @Override public
synchronized String getResult() { // TODO Auto-generated method stub
while(true){ if (!flag) { try { // When realData The object is null Thread wait
System.err.println("**************************"); wait(); } catch
(InterruptedException e) { e.printStackTrace(); } } notify(); return
realData.getResult(); } } }
/** * It's packed into a box “ goods ” * * The purpose of this class is to Access database And return the result * @author yangcheng * */ public
class FeatureRealData implements Data{ private String resultData; // Simulate access to database
public FeatureRealData(String msg){ try { // Simulation database access costs 3 second Thread.sleep(3000); }
catch (InterruptedException e) { e.printStackTrace(); } resultData=
" Real data returned : Data acquisition successful !"; } // Return query results @Override public String getResult() { // TODO
Auto-generated method stub return resultData; } }
/** * This interface is used to specify the method of getting data * @author yangcheng * */ public interface Data { public
String getResult(); }
Technology
Daily Recommendation