Callback Interface:
As we all know, a thread is running, and when it encounters a time-consuming operation (method), it opens another thread, the so-called- async . The callback interface in Java is also bound to be applied with asynchronous loading. The so-called callback interface , that is, the thread runs in a time-consuming operation (method), open a child thread, in the child thread of the operation (method) to the
Another object to complete. The object that is delivered to the completion of the task, after performing the operation, needs to pass the information back to the original object. callback interface techniques must be used at such times. That is, the method that is circulated on the Internet, a calls the C method of B (in order to get the information needed to know), B in turn calls the D method of a (in order to convey (a want to know) the letter
and the rest). The D method (that is, the implementation of the interface) is called the callback method .
The elements that implement the callback method are:
1. Class A is going to use class B as its member variable and refer to it. (Load B When constructing method initialization)
2, class B needs to define an interface, and then pass the information back to the Class A method , the interface as a parameter passed in the past, and then implement an abstract method of the interface, a want to know the information passed in.
3, a when calling the above (red) method, you must implement the method of the interface. That is, the callback method. Complete the communication.
The code is as follows:
//Xiao Ming--he has a question to ask Lao Wang. Public classXiaomingImplementsCallBack {//1. Declare the old King class as his member variableLaowang Laowang; //Xiaoming's method of construction, used to refer to Lao Wang Publicxiaoming (Laowang laowang) { This. Laowang =Laowang; } //Ask Lao Wang questions (time consuming operation) Public voidaskquestion (String question) {//Open Thread NewThread (NewRunnable () {@Override Public voidrun () {//Call Lao Wang's method to get the answer. Laowang.solve (xiaoming. This, question); }}). Start (); //after asking questions, Xiaoming can do the other, that is (async)Play (); } Public voidPlay () {System.out.println ("I went to play ..."); } //callback method Result--that's the answer the old king passed over.@Override Public voidcallback (String result) {System.out.println (result); }}
//Lao Wang--he has the ability to help Xiao Ming solve Problems (method) Public classLaowang {//This is the way to solve the problem, you have to pass the interface as your parameter past Public voidSolve (CallBack callback,string question) {System.out.println ("Xiao Ming asked the question is--->" +question); //simulate the time spent by Lao Wang to solve problems Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); The String result= "Answer is 2"; //parameters for passing an answer to an interfacecallback.callback (Result); } //after you have solved the problem, you have to define an interface to reply to xiaoming with your answer. //Otherwise xiaoming will not be able to receive your answer. Public Interfacecallback{voidcallback (String result); }}
// Test Class Public class Test { publicstaticvoid main (string[] args) { new Laowang (); New xiaoming (Laowang); // the question that Xiao Ming asks Xiaoming.askquestion ("1+1=?") ); }}
java--Callback Interface