Why Is The Sleep Method Given As Static In The Thread Class In Java?


3 Answers

Carry Niggan Profile
Carry Niggan answered

Having said that, and to not further my take about the endless debate on Modafinil's https://afinil.com/nootropics/cheap-sublingual-modafil-md-200-mg/ non therapeutic use, I would like to highlight the inability of this otherwise highly useful drug to improve alertness or wakefulness the way it would be required for keeping one AWAKE AND MEANINGFULLY PRODUCTIVE AND STABLE for long. That combination is hard to come by with regular use of modafinil, given how it would disrupt the sleep wake cycle, altering it in undesirable ways.

Virgo Nadia Profile
Virgo Nadia answered
The static methods of this class operate on the currently running thread. The instance methods may be called from one thread to operate on a different thread. start () starts a thread running. stop() stops it by throwing a ThreadDeath error. suspend() temporarily halts a thread. resume() allows it to resume. sleep() makes the current thread stop for a specified amount of time. yield() makes the current thread give up control to any other threads of equal priority that are waiting to run. join() waits for a thread to die.

Iinterrupt() wakes up a waiting or sleeping thread or sets an "interrupted" flag on a non-sleeping thread. A thread can test its own "interrupted" flag with interrupted () or can test the flag of another thread with isInterrupted (). The Object wait() method makes the current thread block until the object's notify() method is called by another thread.
If your thread will be running for a while, you should call the sleep() or yield() methods in order to give other processes a chance to run. This is more important on some systems than on others, but since you can't know for sure which system your applet will be running on, be a considerate thread programmer.

The run() method takes care of changing the background color, using the sleep() method of the Thread class to temporarily suspend the thread and calling repaint() to redisplay the object after each color change.Notice that the call to sleep() is enclosed in a try block and followed by a catch block that's watching for InterruptedException exceptions. You have to catch this exception because the sleep() method throws it. If you fail to catch the exception, your program will not compile.

Regardless of the scheduling algorithm that is being used, you should not make any assumptions about when a thread will be scheduled to run again after it has called yield(). If you want to prevent a thread from being scheduled to run until a specified amount of time has elapsed, you should call the sleep() method of the Thread object. The sleep() method takes an argument that specifies a minimum number of milliseconds that must elapse before the thread can be scheduled to run again.

Answer Question