1

I want to check which thread is working/running.. As 5 thread are working/running then need to pause then-> as 1 is released/complete/stop next queued thread will start/running.

How to do this

2
  • 1
    Can't you keep track inside your program of which thread was currently released to work? Commented Dec 12, 2011 at 9:15
  • Generally you write your algorithms so you don't need to check the state of a thread. It is only useful for detecting bugs in your application and is rarely done. If you think you need to do this its highly likely there is a better way to approach the problem. Commented Dec 12, 2011 at 10:50

2 Answers 2

3

You can get what state a thread is in by using the getState() method which returns an Enum of Thread.States. A thread can only be in one of the following states at a given point in time.

NEW A Fresh thread that has not yet started to execute.
RUNNABLE A thread that is executing in the Java virtual machine.
BLOCKED A thread that is blocked waiting for a monitor lock.
WAITING A thread that is wating to be notified by another thread.
TIMED_WAITING A thread that is wating to be notified by another thread for a specific amount of time.
TERMINATED A thread whos run method has ended.

Thread t = new Thread(); Thread.State e = t.getState(); Thread.State[] ts = e.values(); for(int i = 0; i < ts.length; i++){ System.out.println(ts[i]); } 
Sign up to request clarification or add additional context in comments.

Comments

2

Or you can use thread pool that is part of JDK, such as ExecutorService.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.