线程的状态
线程对象在不同的运行时期有不同的状态,状态信息就存在与State枚举类中。
验证New,Runnable,Terminated
new:线程实例化后还从未执行start()方法时的状态
runnable:线程进入运行的状态
terminated:线程被销毁时的状态
package Seven; public class MyThread extends Thread { public MyThread() { System.out.println("构造方法中的状态:" + Thread.currentThread().getState()); } @Override public void run() { System.out.println("run方法中的状态:" + Thread.currentThread().getState()); } }
package Seven; public class Run { // NEW, // RUNNABLE, // TERMINATED, // BLOCKED, // WAITING, // TIMED_WAITING, public static void main(String[] args) { try { MyThread t = new MyThread(); System.out.println("main方法中的状态1:" + t.getState()); Thread.sleep(1000); t.start(); Thread.sleep(1000); System.out.println("main方法中的状态2:" + t.getState()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
注意:构造方法中的日志时main主线程的
验证Time_waiting
Time_waiting:线程执行了Thread.sleep()
package Seven; public class MyThread extends Thread { @Override public void run() { try { System.out.println("begin sleep"); Thread.sleep(10000); System.out.println(" end sleep"); } catch (InterruptedException e) { e.printStackTrace(); } } }
package Seven; public class Run { // NEW, // RUNNABLE, // TERMINATED, // BLOCKED, // WAITING, // TIMED_WAITING, public static void main(String[] args) { try { MyThread t = new MyThread(); t.start(); Thread.sleep(1000); System.out.println("main方法中的状态:" + t.getState()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
验证Blocked
blocked:出现在某一个线程等待锁的时候
package Seven; public class MyService { synchronized static public void serviceMethod() { try { System.out.println(Thread.currentThread().getName() + "进入了业务方法!"); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }
package Seven; public class MyThread1 extends Thread { @Override public void run() { MyService.serviceMethod(); } }
package Seven; public class MyThread2 extends Thread { @Override public void run() { MyService.serviceMethod(); } }
package Seven; public class Run { // NEW, // RUNNABLE, // TERMINATED, // BLOCKED, // WAITING, // TIMED_WAITING, public static void main(String[] args) throws InterruptedException { MyThread1 t1 = new MyThread1(); t1.setName("a"); t1.start(); MyThread2 t2 = new MyThread2(); t2.setName("b"); t2.start(); //Thread.sleep(1000); System.out.println("main方法中的t2状态:" + t2.getState()); } }
验证waiting
waiting:是线程执行了object.wait()方法后所处的状态
package Seven; public class Lock { public static final Byte lock = new Byte("0"); }
package Seven; public class MyThread extends Thread { @Override public void run() { try { synchronized (Lock.lock) { Lock.lock.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } }
package Seven; public class Run { // NEW, // RUNNABLE, // TERMINATED, // BLOCKED, // WAITING, // TIMED_WAITING, public static void main(String[] args) { try { MyThread t = new MyThread(); t.start(); Thread.sleep(1000); System.out.println("main方法中的t状态:" + t.getState()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }