the_big_blackbox
the_big_blackbox

Reputation: 1196

What does an "asynchronous method" mean in Java?

I am in the process of working through the android billing example for an app. The sample app refers to an asynchronous method. I have had a look on the web and I cant seem to find a good definition, please can someone help with an example. Sample as follows:

 // Start setup. This is asynchronous and the specified listener
 // will be called once setup completes.

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
    public void onIabSetupFinished(IabResult result) {

Is it a method that does not immediately return a result?

Upvotes: 1

Views: 543

Answers (3)

Kedar Mhaswade
Kedar Mhaswade

Reputation: 4695

If I am not mistaken, you are referring to this method startSetup that accepts a final OnIabSetupFinishedListener and supposedly sets up the billing.

What you seem to be confused about, is this rather syntactically obscure feature of Java called the anonymous inner class.

Let me attempt to answer your question to make it easier:

Is it a method that does not immediately return a result?

Yes, sort of (it of course does not return anything for it is a void method). It, simply speaking, is a method that accepts an instance of the interface OnIabSetupFinishedListener and does some of its job asynchronously as stated in the Javadoc and returns nothing:

This will start up the setup process asynchronously.

Thus, this method is similar to what any other void Java method looks like. The only additional implementation information is that some kind of communication is set up between the listener you pass to this method and some other objects.

But that communication is going to happen at a later point in time, not at the time you call this method, startSetup. Thus, what is important is the call site, i.e. how you are going to call this method in your own app. This, hopefully, happens at the time of setting up your app and you need to get it quickly running and hence this method provides a callback mechanism and returns as soon as possible in a synchronous manner without unnecessary delay. This means your calling thread can make progress and the listener you passed to this method can be utilized later in some other thread when an appropriate event occurs.

The confusion also comes in part because of the way anonymous inner classes are typically coded. Thus, your call site may look like the following:

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
   public void onIabSetupFinished(IabResult result) {
      if (!result.isSuccess()) {
         // Oh noes, there was a problem.
         Log.d(TAG, "Problem setting up In-app Billing: " + result);
      }
         // Hooray, IAB is fully set up!
   }
});

Here, you are providing an anonymous implementation of the interface OnIabSetupFinishedListener directly at the call site (without actually creating a separate class implementing that interface, using the construct like class MyListener implements OnIabSetupFinishedListener).

Upvotes: 1

Alberto Gurrion
Alberto Gurrion

Reputation: 116

An asynchronous method is not a typically request/response You can think of this like a promise or something that will reply without pooling. In your case you are creating an anonymous listener that will resolve the promise here

public void onIabSetupFinished(IabResult result){
 //you will eventually get the response here
}

Upvotes: 0

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10288

Yes. In this context, "asynchronous" means that the method will return immediately and execution will continue with the statement following the method call. Sometime later, the onIabSetupFinished(...) method will be called on the listener. This is called a callback. An important consideration with asynchronous callbacks is what thread they are called in. You'll need to refer to the documentation for this API to find that out.

Upvotes: 0

Related Questions