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

2

2 Answers

Rajat Dhande Profile
Rajat Dhande answered

Static variables are the variables which retain their values between the function calls. They are initialized only once their scope is within the function in which they are defined.

for more info in visit this side

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

Anonymous