A callback is a method by which the caller holds a reference to the callee, and the implementation class of the caller interface is returned by the caller as a parameter of the callee.
Interface callback
Public interface CallBack {
/** callback method */
public void Execute (Object ... objects);
}
Caller Local
public class Local implements CallBack, runnable{
Private Remote remote;//The callee as a reference to the caller
Private String msg;
Public Local (remote remote, String msg) {
Super ();
This.remote = remote;
this.msg = msg;
}
@Override
public void Execute (Object ... objects) {
The returned message
System.out.println (Objects[0]);
System.out.println (Thread.CurrentThread (). GetName ());
Thread.interrupted ();
}
public void SendMessage () {
System.out.println (Thread.CurrentThread (). GetName ());
Thread thread = new thread (this);
Thread.Start ();
SYSTEM.OUT.PRINTLN ("Send Message");
}
public static void Main (string[] args)
{
Local local = new local (new Remote (), "Hello");
Local.sendmessage ();
}
@Override
public void Run () {
Remote.executemessage (msg, this);
}
}
Callee Remote
public class Remote {
public void ExecuteMessage (String msg,callback CallBack) {
SYSTEM.OUT.PRINTLN (msg);
System.out.println ("received the command from the local");
Callback.execute (New string[]{"Hello Local"});
}
}
Java callback function