Implementation of a simple java callback function, java callback function

Source: Internet
Author: User

Implementation of a simple java callback function, java callback function
The three functions involved in the callback function registration callback function
Callback Function
Response callback function
A simple explanation is that when you go to a store to buy things, you just have no goods, so you left your phone number at the clerk. After a few days, there will be goods in the store, the clerk calls you, and then you get the goods from the store after receiving the call. In this example, your phone number is called a callback function. If you leave the phone number to the clerk, it is called a registration callback function. Later, the store purchased the event called triggering a callback Association, the clerk calls you to call the callback function. When you pick up the goods in the store, it is called a response callback event. The answer is complete. Click to open the link

The code implementation first has an interface CallBack, which is of course not feasible. It is mainly used to define the business logic in advance.

Public interface CallBack {/** response CallBack function */public void slove ();}

Then there is the implementation of the above interface, registration callback and response callback implementation class
Public class A implements CallBack {B B = new B (); @ Override/** (non-Javadoc) * @ see CallBack # slove () * response callback function */public void slove () {System. out. println ("the problem is solve! ");}/** Register callback function */public void askQuestion () {System. out. println (" ask B solve the problem! ");/** Do other things by yourself */new Thread (new Runnable () {@ Overridepublic void run () {System. out. println ("A want to do another thing! ");}}). Start ();/** ask B to solve this problem */this. b. call (this);}/** test */public static void main (String [] args) {A a = new A ();. askQuestion ();}}

Then there is the class that implements the callback function.
Public class B {/** CallBack function */public void call (CallBack a) {/** B help a solve the priblem */System. out. println ("B help a solve the problem! ");/** Call back */a. slove ();}}

Test Results



Who can help me write a java callback function example?

Hello, I wrote a very simple code, and you will understand it at a glance.

Public class {
Int value = 1001;

Public A () {// The constructor of A. Execute the exec () method of B and call the getValue () method of A. This is called callback.
B B = new B (this );
B .exe c ();
}

Public int getValue (){
Return value;
}

Public static void main (String [] args ){
New A (); // callback has already occurred in the constructor, so no more code is needed
}
}

Class B {
Private A;

Public B (A ){
This. a =;
}

Public void exec (){
System. out. println (a. getValue (); // The getValue () method of a is called here.
}
}

What is the callback function in JAVA? It is best to include an example to describe how to make it easier ,,

For example, if you write two classes A and B, and each generates a corresponding instance a and B, there is A method fa in a (this method is only responsible for generating two random numbers) you need to call a method fb in B (this method calculates each of the two numbers) to sum, after obtaining the result from fb, you need to call another method (ffa) of a (this method is used to display the obtained result). In this case, ffa is called the callback function (which is called back)
Why is it so troublesome? If fb does not return results directly, imagine that if fb takes a long execution time, fa will wait until the results are executed, and fa will not execute any further, in this case, the callback function is implemented using the callback function. Because the callback function is generally called asynchronously by the system, the method fa will directly execute the function after calling fb without waiting. For example, two random numbers can be generated to fb, after obtaining the results in sequence, fb calls the ffa function through the callback mechanism to display the results. In fact, it is used to generate random numbers and calculate random numbers asynchronously. It seems that fa and fb are two independent threads to improve program efficiency.

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.