Simple parsing of three Java call modes-synchronous, asynchronous, callback

Source: Internet
Author: User

There are three ways to call between modules:

1. Synchronous call

The synchronous invocation is simple, calling method B directly, and must wait until method a executes before proceeding with the original program.

2. Asynchronous invocation

An asynchronous invocation is an implementation of program B in a program, and when B runs, it can invoke the implementation in the a program when it satisfies the condition. Examples Show

 Public Interfacefood{voideat ();} Public classCowImplementsfood{@overridevoideat () {SYSO ("Eat cow"); }                    } Public classtiger{PrivateFood and food ;  Public voidSetfood (food food) { This. Food =Food ; }        voidhungry () {SYSO ("Hungry");        if (eat!=null) food.eat (); }                   Public void StaticMain (string[] args) {Tiger Tiger=NewTiger (); Tiger.setfood (NewCow ());         Tiger.hungry (); }    }

The tiger is hungry, if there is food can eat it, now the cow is equivalent to program A to achieve this interface,

The tiger is the equivalent of program B, which contains this interface,

Now send the cow to the mouth of the tiger, is Setfood

The tiger is hungry to eat cattle, this time called a program implementation of this interface.

3. Callback

The Android program will definitely be familiar, the callback is actually the above two combination.

For example, we are instantiating a button class, it will set a onclicklistener, which is equivalent to the above asynchronous call, but this asynchronous call is triggered by the button itself, button of the PerformClick () The simulated click event then executes the custom Onclicklistener.

Simple parsing of three Java call modes-synchronous, asynchronous, 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.