Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java multithreading interview: sleep, wait, notify, yield - which one is a callback?

This is one question, asked to me a interview, on which I have no idea what he is asking.
If you can help on the same:

sleep, wait, notify, yield - which one is a callback?

like image 689
lowLatency Avatar asked Feb 16 '23 08:02

lowLatency


2 Answers

None of the methods you list are callbacks. The entire Thread class contains only one user-overridable method, and that is run, which may be considered a callback method for that class because it is called by Thread's internals. However, a best practice is not to extend Thread at all. Supply your own Runnable implementation, which has its callback run method.

like image 90
Marko Topolnik Avatar answered Feb 18 '23 14:02

Marko Topolnik


None of those look like traditional callbacks. A callback function/method is something you register to be called once an operation is complete (possibly asynchronously if the task is scheduled in another thread).

Sleep, wait and yield essentially block execution until their conditions are met. Notify wakes threads blocked by wait.

like image 20
Kainsin Avatar answered Feb 18 '23 13:02

Kainsin