2

What's a difference between Callback and event in Async and Sync programming ?

What I've understood about Sync coding standard is, callback is a piece of which gets executed after every event piece of code ? And next event won't be called until the last callback piece of code is executed. Is it correct ?

Secondly, In ASync coding, Once event piece of code is executed, callback piece of code is called. And no matter that last callback piece of code is executed or not, it will call the next event and respective callback and so on. Is it correct ?

What, I'm thinking is callback can behave as acknowledgment to the event piece of code execution (could be other sense too).

I was reading this blog https://www.geeksforgeeks.org/asynchronous-synchronous-callbacks-java/

Where I found below statment about Async and could not understand properly ?

An Asynchronous call does not block the program from the code execution. When the call returns from the event, the call returns back to the callback function.

Does it means, When event code is executed, the callback code is called ?

// Java program to illustrate synchronous callback 
interface OnGeekEventListener { 

    // this can be any type of method 
    void onGeekEvent(); 
} 

class B { 

    private OnGeekEventListener mListener; // listener field 

    // setting the listener 
    public void registerOnGeekEventListener(OnGeekEventListener mListener) 
    { 
        this.mListener = mListener; 
    } 

    // my synchronous task 
    public void doGeekStuff() 
    { 

        // perform any operation 
        System.out.println("Performing callback before synchronous Task"); 

        // check if listener is registered. 
        if (this.mListener != null) { 

            // invoke the callback method of class A 
            mListener.onGeekEvent(); 
        } 
    } 

    // Driver Function 
    public static void main(String[] args) 
    { 
        B obj = new B(); 
        OnGeekEventListener mListener = new A(); 
        obj.registerOnGeekEventListener(mListener); 
        obj.doGeekStuff(); 
    } 
} 

class A implements OnGeekEventListener { 

    @Override
    public void onGeekEvent() 
    { 
        System.out.println("Performing callback after synchronous Task"); 
        // perform some routine operation 
    } 
    // some class A methods 
} 

Last doubt, In above code, if event code (doGeekStuff())is being processed, And another request comes too to execute event code, the above code will fail or wait for current event processing to be done ?

Maria
  • 452
  • 1
  • 6
  • 20
  • I don't know that there is any official definition of [callback](https://stackoverflow.com/questions/58841976/is-the-observer-object-oriented-design-pattern-just-a-form-of-implementing-a-cal/58845660#58845660). People mean different things when they use that word. – jaco0646 Mar 29 '20 at 14:12
  • was my answer any help? – Abhinav Chauhan Jun 26 '20 at 03:42

1 Answers1

3

An event can be anything like you touching the phones screen , clicking a button , pressing a key , lost connection etc.

And a callback is response to those events

In event driven programming we get callback method which are called when some event happens.

Event driven programming is normally implemented with a design pattern called a observer Pattern.

from head first design patters Callbacks are basically implemented by implementing an interface by the observer the event driving object calls the callbacks methods on observer at every event and those methods are for sure defined by observer because it implement that interface, interface is kind of a contract between event driving object and observer (hey observer i only gives you callback only if you implement that interface)

In your code that interface is

interface OnGeekEventListener { 
// this can be any type of method 
void onGeekEvent(); 
} 

the onGeekEvent(); will be called on observing objects when the event occurs.

In above code class B is a subject/producer when you call below method on an instance of class B passing your object like classBobj.registerOnGeekEvent(classAobj) it registers you as a listener/observer/consumer, and put your object in its mListener member variable , now whenever an event occurs it can call onGeekEvent() method on that object.

 public void registerOnGeekEventListener(OnGeekEventListener mListener) 
{ 
    this.mListener = mListener; 
} 

Synchronous callback when the code inside those methods run on the same from which the event object calls them it is synchronous.

So if the code is called on the same thread you can't execute in parallel two events because thread is the same, that is why there is written "until lastcallback is executed"

example -> it is like you have only one kid and you send him to take water , then you can send him to take food until he comes back with water(same as you can't execute another callback)

Asynchronous callback when code inside those methods runs of a different thread, or maybe on some other computer (in later case you are the observer for that other compute)

so if you spawn a new thread at every callback , then all those callbacks can be executed in parallel

example -> It is like you have many kids you send one to take water , while he is getting water so send other one to take food , then other one to bring you a coffee and so on

Community
  • 1
  • 1
Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
  • Are observers callback instances (Class A) ? – Maria Mar 29 '20 at 12:21
  • yes an instance of class A will be called an observer and the subject can call onGeekEvent() on it when an event happens, but only if the object subscribes to get those callbacks – Abhinav Chauhan Mar 29 '20 at 12:23
  • if you're answered you can mark it answered , if not i am here to help you – Abhinav Chauhan Mar 29 '20 at 12:24
  • Which one is Observer Instance and which one is subject Instance in above code ? – Maria Mar 29 '20 at 12:28
  • So you mean doGeekStuff() is an event generator and onGeekEvent() is Observer ? – Maria Mar 29 '20 at 12:49
  • No events can be generated in may way as i mentioned above , and an object of class b is just aware of those events and it call tell about those event to observers , in above code doGeekStuff() is just a utility method to show you that we are going geek stuff, it is not an event generator , class B object calling it when event occurs and it calls mListner.onGeekEvent() which is a callback , they defined doGeekStuff because in main method we can't say mListner.onGeekEvent as it is instance field and can't be accessed in static context – Abhinav Chauhan Mar 29 '20 at 12:54
  • for example clicking a button is an event but a button does not generate event it is you who triggered event via mouse and the button is just aware of that it was clicked , so when the button is clicked it calls the callback method on its observers – Abhinav Chauhan Mar 29 '20 at 12:57
  • 1) mListner.onGeekEvent can be called from main method too, but not being done becasue it should called to peform some callback action. And callBack is dervied from event listner. 2) doGeekStuff() needs to be triggered on every event (Where we can perform some operations if required), so Event ->register Event->doGeekStuff()-> onGeekEvent() I hope am correct – Maria Mar 29 '20 at 13:21
  • 2
    i recommend please get a good course of a good book, everything cannot be explained here , if you have basic understanding of java you should know that mListener is an instance variable you can't directly access from a static block (main method is a static block) and listener don't deliver callback it is the producer , sorry i can't explain more than that , i hope my answer has done little bit of help,, asyn , snyc etc are little advance stuff, i encourage you to learn basic first .. All the bast @Maria – Abhinav Chauhan Mar 29 '20 at 13:34
  • That I know, I was meant that mListner.onGeekEvent() can be done in main context (after creating an instance of A) to call direct callBack – Maria Mar 29 '20 at 13:57
  • Sync and ASync are tied to Event+callBack terminology. But it happens in different ways in Sync and ASync. – Maria Mar 29 '20 at 13:59
  • yes but a producer does not know anything about A so how can it create an instance of it , it just knows that it is getting something which has implemented required interface, class B can be already defined somewhere lets say few years ago, then how can you create A object in its main method, that class won't have a main method in first place, it is here just to show you, how it works – Abhinav Chauhan Mar 29 '20 at 14:01
  • "producer does not anything about A", Really..? B (Event Listner) + A (callback response to event Listner) has to be tied together so that Sync or Async could be achieved. I was pointing to your comment "f you have basic understanding of java you should know that mListener is an instance variable you can't directly access from a static block (main method is a static block) and listener don't deliver callback it is the producer ", because if thats a case, callBack can be called independently. :) – Maria Mar 29 '20 at 14:05
  • But its okay, I've got some fair ideabut not exactly, thanks for your help..! – Maria Mar 29 '20 at 14:06
  • if you think that my answer was any help , may you mark it answered now – Abhinav Chauhan Apr 19 '20 at 15:57