Difference between Callback and event

Viewed 843

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 ?

1 Answers

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

Related