A simple Java callback example

Source: Internet
Author: User

Simple demo, hope to help you understand

First, you need a listener to listen.

Listener. Java

package bbbb;public interface Listener {public void onComplete();}

Then a request is required to request the answer.

Requset. Java

Package BBBB; public class request {public request () {New answer (). thinking (new listener () {@ overridepublic void oncomplete () {// todo auto-generated method stubsystem. out. println ("thinking successful... ");}});}}

Finally, an answer is needed to answer the question.

Answer. Java

package bbbb;import java.awt.List;public class Answer {public void Thinking(final Listener l){new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}l.onComplete();}}).start();}}

Main. Java

package bbbb;public class Main {public static void main(String arg[]){new Request();}}

In a simple demo, the general principle is to ask an answer for a request. After answer finishes thinking, the answer is placed in the listener .. Then the request will know the answer ..

Welcome to the discussion.

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.