Thread objects have different states at different times of operation, and state information exists in the class State enumeration class of the thread inner
Public enum State { /** * New status is the status when the start () method has never been executed after a thread instantiation */ new, /** * runnable State is the state that the thread is running into. */ RUNNABLE, /** * Blocked state occurs when a thread is waiting for a lock. */ BLOCKED, /** * Waiting is the state of the thread after the Object.wait () method is executed */ waiting, /** * timed_ Waiting executes the Thread.Sleep () method on behalf of the thread, * Waits, waits for time to arrive, and continues to run down. */ Timed_waiting, /** * terinated is the state at which the thread was destroyed, the thread completely executed */ TERMINATED; } The method can get the status of the thread public state GetState () { //Get the current thread states return Sun.misc.VM.toThreadState ( threadstatus); }
1. Verification of new, runnable and terminated
The following code validates all the state values of a thread, knowing that the state of the thread helps the programmer to monitor the situation of the thread object, such as which threads have never been started, which threads are executing, which threads are blocking, which threads are waiting, which threads have been destroyed, and so on. These are information related to the thread life cycle.
The first is to verify the new, runnable, and terminated states, the new state is the state when the start () method has never been executed after the thread is instantiated, and the runnable state is the state in which the thread is running, and terinated the state when the thread is destroyed.
public class MyThread extends the Thread {public MyThread () { System.out.println ("state in the construction method:" + Thread.CurrentThread (). GetState ()); } @Override public Void Run () { System.out.println (state in the "Run Method:" + Thread.CurrentThread (). GetState ());} }
public class Run {public static void Main (string[] args) throws interruptedexception { MyThread thread = new Myth Read (); SYSTEM.OUT.PRINTLN ("State 1 in the Main method:" +thread.getstate ()); Thread.Sleep (+); Thread.Start (); Thread.Sleep (+); System.out.println ("State 2 in the Main method:" + thread.getstate ());} }
State in the construction method: RUNNABLE The state 1:new in the main method State in the Run method: RUNNABLE The state 2:terminated in the main method |
2. Verification timed_waiting
Timed_waiting represents a thread that executes the Thread.Sleep () method, waits for the time to arrive, and continues to run down.
public class MyThread extends Thread { @Override public void Run () { try { System.out.println ("Begin Sleep "); Thread.Sleep (4000); System.out.println (" End Sleep"); } catch (Interruptedexception e) { e.printstacktrace ();}} }
public class Run {public static void Main (string[] args) throws interruptedexception { MyThread thread = new My Thread (); Thread.Start (); To verify the timed_waiting, the sleep time here is less than the sleep time Thread.Sleep (+) in the Run method ; System.out.println ("The state in the Main method:" + thread.getstate ());} }
Begin Sleep State in the Main method: Timed_waiting End Sleep |
Note: The Run class sleep time is less than the Mythread class sleep time, otherwise the result is:
Begin Sleep End Sleep State in the Main method: TERMINATED |
3. Verification blocked
The blocked state occurs when a thread waits for a lock.
public class MyService {public synchronized static void Servicemethod () { try { System.out.println ( Thread.CurrentThread (). GetName () + "entered the Business Method!" " ); Thread.Sleep (); } catch (Interruptedexception e) { e.printstacktrace ();}} }
public class MyThread1 extends Thread { @Override public void Run () { myservice.servicemethod (); }}
public class Run {public static void Main (string[] args) throws interruptedexception { MyThread1 t1 = new Mythrea D1 (); T1.setname ("AA"); T1.start (); SYSTEM.OUT.PRINTLN ("T1 state in the Main method:" + t1.getstate ()); MyThread1 t2 = new MyThread1 (); T2.setname ("BB"); T2.start (); Thread.Sleep (+); System.out.println ("T2 state in the Main method:" + t2.getstate ());} }
AA has entered the business Method! The T1 state in the Main method: RUNNABLE The T2 state in the Main method: BLOCKED BB enters the business method! |
From the console printing results, the T2 thread has been waiting for T1 to release the lock, so T2 's state was blocked
4. Verification waiting
The state waiting is the state that the thread was in after the Object.wait () method was executed.
public class MyLock {public static final byte lock = new byte ("0");}
public class MyThread extends Thread { @Override public void Run () { try { synchronized (mylock.lock) {
mylock.lock.wait (); } } catch (Interruptedexception e) { e.printstacktrace ();}} }
public class Run {public static void Main (string[] args) throws interruptedexception { MyThread t = new Mythrea D (); T.start (); Thread.Sleep (+); If not, the state of T is runnable System.out.println ("T state in the Main method:" + t.getstate ());} }
| The T state in the Main method: Waiting |
The State enumeration value of the thread after the Wait () method is executed is waiting
Java multithreaded Programming 7--supplements additions--thread state (new,runnable,terminated,timed_waiting,waiting,blocked)