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 ?