Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a thread is sleeping?

Is there any way to check if a given thread is sleeping?

like image 358
Sameek Mishra Avatar asked Mar 17 '11 07:03

Sameek Mishra


People also ask

How can I tell if a thread is sleep?

You can call Thread. getState() on and check if the state is TIMED_WAITING . Note, however that TIMED_WAITING doesn't necessarily mean that the thread called sleep() , it could also be waiting in a Object.

How can I tell if a thread is alive?

A thread is alive or running if it has been started and has not yet died. To check whether a thread is alive use the isAlive() method of Thread class. It will return true if this thread is alive, otherwise return false .

How long should a thread sleep?

If it is a UI worker thread, as long as they have some kind of progress indicator, anywhere up to half a second should be good enough. The UI should be responsive during the operation since its a background thread and you definitely have enough CPU time available to check every 500 ms.

What happens to a thread when it sleeps?

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.


1 Answers

You can call Thread.getState() on and check if the state is TIMED_WAITING.

Note, however that TIMED_WAITING doesn't necessarily mean that the thread called sleep(), it could also be waiting in a Object.wait(long) call or something similar.

like image 152
Joachim Sauer Avatar answered Oct 10 '22 04:10

Joachim Sauer