Java callback function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.