Java callback mechanism analysis and java callback Analysis

Source: Internet
Author: User

Java callback mechanism analysis and java callback Analysis

In fact, the callback mechanism is often used in actual use. But the funny thing is that we have never been able to have a clear understanding of the so-called callback concept.

Recently, when I read some books, I often mention the callback concept from time to time. Well, I just took the time to make a brief summary to deepen my impression and understanding ~


The written and normative interpretations of Callbacks are as follows:

In computer programming, a callback function is a reference to a piece of executable code that is passed to other code through function parameters. This design allows underlying code calls to subprograms defined at a higher level.


I don't know how people feel about the concept of "official". I am a "one-pack "~

So I read some other people's articles on the Internet and found that someone else made a metaphor long ago:

One day, I called you to ask questions. Of course it was a problem. ^ _ ^. You couldn't find a solution at the moment, and I couldn't hold the phone and wait.

So we agreed: After you come up with a solution, you can call me to notify me. In this way, I will stop calling to do other things.

After XX minutes, my cell phone rang, and you said with high enthusiasm that the problem had been fixed, so you should handle it like this. The story ends here.


This example illustratesAsynchronous + callbackProgramming mode.

When you call a mobile phone, the result is a "Callback" process;

My mobile phone number must be told before. This is the registration callback function;

My mobile phone number should be valid and the mobile phone can receive your call. This is the callback function that must comply with the interface specifications.


In this case, you should have a general understanding of callback. Now, it is a year-long job-hopping peak at the beginning of the year.

The employee resignation and job-hopping can also help us to understand the "Callback.


The common reason for job-hopping is salary, so:

Assume that we are an employee and a new year, and our salary is still poor and anxious.

What should we do? Jump? What if the salary needs to be adjusted after the first hop?

Therefore, we decided to "negotiate" with the BOSS and make the final decision based on the results.

In this case, it is actually a callback.

We break down this problem from the perspective of a programmer:


1. As an employee, we want to make a final decision based on the discussion with the BOSS about salary adjustment.

/*** Employee class ** @ author hql 2015/03/03 */public class Employee {SalaryListener listener; void setSalaryListener (SalaryListener listener) {this. listener = listener;} void makedemo-() {int salary = listener. getNewSalary (); if (salary> = 10000) {System. out. println ("satisfied with salary adjustment, continue to struggle! ");} Else {System. out. println (" Oh, bye! ");}}}

2. The employee's final decision is based on the salary adjustment result. At the same time, the salary adjustment result is provided by the BOSS.

Therefore, if we want to get this result, we need to provide a "channel" for the BOSS to give us a "statement", ^ _ ^ ~ This channel is our defined standard-interface.

/*** Callback interface ** @ author hql 2015/03/03 */public interface SalaryListener {/** this is actually a way for you to reply your salary adjustment inquiry to the BOSS * According to the BOSS this way gives you a result, you can use this result to make a decision */public int getNewSalary ();}

3. BOSS determines your salary adjustment result based on your work performance. In addition, it is worth noting that:

The final result provided by the BOSS must be returned to you based on the "channel" provided by you so that you can receive the result accurately and make a correct decision based on the result (the program runs correctly ).

/***** Boss class ** @ author hql 2015/03/03 */public class Boss implements SalaryListener {/** Boss received your question about salary adjustments, reply to you based on your question (the interface you provided) */@ Overridepublic int getNewSalary () {return 10000 ;}}

4. The next step is the real "negotiation" (test class)
/*** Callback function test class ** @ author hql 2015/03/03 */public class TestCallBackFunc {public static void main (String [] args) {// forced Employee me = new Employee (); // The salary adjustment result BOSS Boss = new boss () given by the Boss for the forced Employee (); // The hard-pressed employee obtained the result me. setSalaryListener (boss); // callback, make the decision me. makedeworkflow ();}}

Here, as an employee, we need to get a result about salary adjustment, which is determined by the BOSS.

So first, the employee raised this question to the BOSS and hoped to get the result. This process is: the employee calls the BOSS.

While the BOSS received the question raised by the employee and handled it. At this time, he would like to tell the employee the handling result. The process here is: The BOSS call employee.

So when the problem is solved completely, it is when the BOSS receives the call from the employee and calls back to the employee. This is the so-called callback!


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.