Java synchronous calls, callbacks, and asynchronous invocations.

Source: Internet
Author: User
Tags anonymous

There is always a certain interface between software modules, from the way of invocation, they can be divided into three categories: synchronous call, callback and asynchronous call.

Synchronous call: A blocking call in which the caller waits for the other party to execute before returning, which is a one-way call;

Callback: A bidirectional invocation pattern, that is, the caller also invokes the interface of the other when the interface is invoked;

Asynchronous invocation: A mechanism similar to a message or event, but its invocation is in the opposite direction, when a service of an interface receives a message, or an event occurs, proactively notifies the client (that is, the interface that invokes the client).

Callback and asynchronous invocation are very closely related: Use callbacks to implement the registration of asynchronous messages, and to implement notification of messages by asynchronous invocation.

========================================================

Use the example in Java:

Package callbackexample; Public interface Icallback {//Required callback method public void Postexec ();}

Another class:

Package callbackexample; public class FooBar {//Combinatorial aggregation principle private icallback callBack; public void Setcallback (Icallback callBack) {this.callback = c Allback; Dosth (); public void Dosth () {callback.postexec ();}}

The second class, inside the test class, is an anonymous class:

Package callbackexample; public class Test {public static void main (string[] args) {FooBar foo = new FooBar (); Foo.setcallback (new Icallback () { public void Postexec () {System.out.println ("a reference to an object that is implemented in the test class but not subject to test, called by the Foobar object);}"; } }

Code of Appeal:

1. Two classes: Anonymous class and Foobar

2. Anonymous class Implementation interface Icallback (implemented in the form of anonymous classes in the main method of test testing)

3.FooBar has a function with a parameter of Icallback interface type Setcallback (Icallback o)

4. Anonymous class runtime calls the Setcallback function in Foobar to pass parameters in itself

5.FooBar an anonymous class has been obtained, the method in the Icallback interface implemented in the anonymous class can be recalled at any time

==================================

1. First, the concept of a callback method is not the same as the concept of "construct method", and it does not refer to a method in Java that has special meaning or purpose.
2. Call it the "callback" of the method is more appropriate, it refers to a method of invocation. Any method that is called "callback" can be called a "callback method".
3. The callback of a method usually occurs during the use of the Java interface and abstract classes.
Suppose there is an interface named Icallback which has a method named Postexec ()
A class of MyClass implements the interface, which means that the postexec () method must be implemented. Now there is another class Foobar it has a method Setcallback (Icallback callBack), and the Setcallback method calls CallBack () method.
If we now use a MyClass instance of MyClass and take it as a parameter into the Setcallback (Icallback callBack) method, we'll say Setcallback (Icallback callBack) Method recalls the Postexec () method of the MyClass.











China's latest Chinese women's shoes net, from Taobao cat month sales of thousands of women's shoes yo:
www.taotuike.cn


Related Article

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.