Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Thread.sleep() works internally

Let's say I have a thread T and it is holding one resource R. If I call Thread.sleep() on the current thread i.e T, will it release the resource R (to let other threads use it) before going to sleep or not? Or it will hold that resource and again when it will awake it will use the resource R and after finishing the work will it release it?

like image 576
user3766199 Avatar asked Jun 23 '14 05:06

user3766199


People also ask

How does thread sleep work?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Does thread sleep use CPU?

To be more clear: Threads consume no CPU at all while in not runnable state. Not even a tiny bit. This does not depend on implementation.

What happens when a sleep is called on thread?

sleep() interacts with the thread scheduler to put the current thread in wait state for specified period of time. Once the wait time is over, thread state is changed to runnable state and wait for the CPU for further execution.

Does sleeping on a thread consume resources?

For most cases, the resources consumed by a sleeping thread will be its stack space. Using a 2-threads-per-connection-model, which I think is similar to what you're describing, is known to cause great scalability issues for this very reason when the number of connections grow large.


5 Answers

First of all, Thread.sleep() is Blocking library method. Threads may block, or pause, for several reasons: waiting for I/O completion, waiting to acquire a lock, waiting to wake up from Thread.sleep, or waiting for the result of a computation in another thread. When a thread blocks, it is usually suspended and placed in one of the blocked thread states.

So, when you call the sleep() method, Thread leaves the CPU and stops its execution for a period of time. During this time, it's not consuming CPU time, so the CPU can be executing other tasks.When Thread is sleeping and is interrupted, the method throws an InterruptedException exception immediately and doesn't wait until the sleeping time finishes.

The Java concurrency API has another method that makes a Thread object leave the CPU. It's the yield() method, which indicates to the JVM that the Thread object can leave the CPU for other tasks. The JVM does not guarantee that it will comply with this request. Normally, it's only used for debug purposes.

One of the confusion with sleep() is that how it is different from wait() method of object class.

The major difference between wait and sleep is that wait() method release the acquired monitor when thread is waiting while Thread.sleep() method keeps the lock or monitor even if thread is waiting.

like image 176
Nasruddin Avatar answered Sep 28 '22 11:09

Nasruddin


From this Javamex article:

The Thread.sleep() method effectively "pauses" the current thread for a given period of time. We used it in our very first threading example to make threads display a message periodically, sleeping between messages. From the outset, it's important to be aware of the following:

  • it is always the current thread that is put to sleep;

  • the thread might not sleep for the required time (or even at all);

  • the sleep duration will be subject to some system-specific granularity, typically 1ms;

  • while sleeping, the thread still owns synchronization locks it has acquired;

  • the sleep can be interrupted (sometimes useful for implementing a cancellation function); calling sleep() with certain values can have some subtle, global effects on the OS (see below), and vice versa, other threads and processes running on the system can have subtle effects on the observed sleep duration.

like image 29
rachana Avatar answered Sep 28 '22 11:09

rachana


The thread which is going to sleep will hold the lock(not release resource) while it sleeps. A sleeping thread will not even be scheduled for the time it sleeps (or until it is interrrupted and then it wakes up)

like image 20
TheLostMind Avatar answered Sep 28 '22 13:09

TheLostMind


If your resource R is java monitor, then there are only two ways to release it:

  • exit synchronized block
  • call wait on owned monitor
like image 24
Sergey Alaev Avatar answered Sep 28 '22 11:09

Sergey Alaev


Javadoc says - sleep(): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers

The Thread.sleep() method essentially interacts with the thread scheduler to put the current thread into a wait state for the required interval. The thread however does not lose ownership of any monitors.

In order to allow interruption, the implementation may not actually use the explicit sleep function that most OS's provide.

If the current thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of Thread class, then its interrupt status will be cleared and it will receive an InterruptedException.

like image 39
SparkOn Avatar answered Sep 28 '22 13:09

SparkOn