Amit
Amit

Reputation: 229

how to check thread is running

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

Upvotes: 1

Views: 3462

Answers (2)

edwardw
edwardw

Reputation: 14002

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

Upvotes: 2

Andrea Girardi
Andrea Girardi

Reputation: 4437

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]); 
  }   

Upvotes: 3

Related Questions