Computer Science, asked by Arulkumaran4332, 1 year ago

Differentiate between sleep() and interrupt() method.

Answers

Answered by Raju2392
1

Answer:

The major difference is that wait() releases the lock or monitor while sleep() doesn't releases the lock or monitor while waiting. wait() is used for inter-thread communication while sleep() is used to introduce pause on execution, generally.

Answered by Jasleen0599
0

Differentiate between sleep() and interrupt() method.

  • When you need this thread to take a brief break, use the sleep() function.
  • The thread is put to sleep for a predetermined period of time using the static sleep() function of the Thread class. If a thread is interrupted by another thread, such as another Thread, the sleep() method throws an InterruptedException.
  • With the interrupt() function, we may halt the execution of any threads that are dozing off or waiting for anything by displaying an InterruptedException. With the use of the interrupt() method of the Thread class, a thread that is dormant or waiting can be terminated.
  • An interrupt tells a thread it needs to stop what it's doing and start something new. The precise way a thread reacts to an interrupt is left to the programmer, however thread termination is a relatively typical response.
  • The Thread class's Sleep () method is used to temporarily halt the execution of the currently running Thread. The sleeping period is measured in milliseconds by the Sleep () method. When we utilise the Sleep () method and resume the execution from where it left off, the monitor's ownership is not lost.

#SPJ2

Similar questions