这里介绍一下java中关于线程状态的知识,主要通过代码演示各种状态出现的时机。少年时我们追求激情,成熟后却迷恋平庸,在我们寻找,伤害,背离之后,还能一如既往的相信爱情,这是一种勇气。每个人都有属于自己的一片森林,迷失的人迷失了,相逢的人会再相逢。
java多线程的状态
在java1.5中,Thread.State枚举类包含了线程的所有状态。以下是jdk关于线程的几种状态的说明:
1、NEW:A thread that has not yet started is in this state. 2、RUNNABLE:A thread executing in the Java virtual machine is in this state. 3、BLOCKED:A thread that is blocked waiting for a monitor lock is in this state. 4、WAITING:A thread that is waiting indefinitely for another thread to perform a particular action is in this state. 5、TIMED_WAITING:A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. 6、TERMINATED:A thread that has exited is in this state.
以下我们通过代码来看一下各个状态切换出现的场景。
一、NEW、RUNNABLE、TERMINATED状态
package com.linux.huhx.thread3.threadState_1; /** * @Author: huhx * @Date: 2017-10-31 下午 2:44 * State: NEW, RUNNABLE, TERMINATED */ public class StateRun1 { public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); System.out.println("name " + Thread.currentThread().getName() + " 1 " + myThread.getState()); // name main 1 NEW Thread.sleep(1000); myThread.start(); Thread.sleep(1000); System.out.println("name " + Thread.currentThread().getName() + " 2 " + myThread.getState()); // name main 2 TERMINATED } } class MyThread extends Thread { public MyThread() { System.out.println("name " + Thread.currentThread().getName() + " constructor " + Thread.currentThread().getState()); // name main constructor RUNNABLE } @Override public void run() { System.out.println("name " + Thread.currentThread().getName() + " run method " + Thread.currentThread().getState()); // name Thread-0 run method RUNNABLE } } /* name main constructor RUNNABLE name main 1 NEW name Thread-0 run method RUNNABLE name main 2 TERMINATED */
二、TIMED_WAITING状态
package com.linux.huhx.thread3.threadState_1; /** * @Author: huhx * @Date: 2017-10-31 下午 2:51 * STATE: TIMED_WAITING */ public class StateRun2 { public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(1000); System.out.println("main " + myThread.getState()); // main TIMED_WAITING } static class MyThread extends Thread { @Override public void run() { try { System.out.println("begin sleep."); Thread.sleep(10 * 1000); System.out.println("end sleep." + Thread.currentThread().getState()); // RUNNABLE } catch (InterruptedException e) { e.printStackTrace(); } } } } /* begin sleep. main TIMED_WAITING end sleep.RUNNABLE */
三、TIMED_WAITING, BLOCKED状态
package com.linux.huhx.thread3.threadState_1; import java.util.concurrent.TimeUnit; /** * @Author: huhx * @Date: 2017-10-31 下午 2:57 * STATE: TIMED_WAITING, BLOCKED */ public class StateRun3 { public static void main(String[] args) throws InterruptedException { Thread1 threadA = new Thread1(); threadA.setName("A"); threadA.start(); Thread2 threadB = new Thread2(); threadB.setName("B"); threadB.start(); TimeUnit.SECONDS.sleep(2); System.out.println("In main threadB " + threadB.getState()); System.out.println("In main threadA " + threadA.getState()); } static class Thread1 extends Thread { @Override public void run() { Service.serviceMethod(); } } static class Thread2 extends Thread { @Override public void run() { Service.serviceMethod(); } } } class Service { public synchronized static void serviceMethod() { try { System.out.println(Thread.currentThread().getName() + " enter service method."); TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } /* A enter service method. In main threadB BLOCKED In main threadA TIMED_WAITING B enter service method. */
四、WAITING状态
package com.linux.huhx.thread3.threadState_1; import com.linux.huhx.thread2.ThreadB; /** * @Author: huhx * @Date: 2017-10-31 下午 3:11 * STATE: WAITING */ public class StateRun4 { public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(1000); System.out.println("In main thread state: " + myThread.getState()); // WAITING new MyThreadB().start(); } static class Lock { public static final Byte lock = new Byte("0"); } static class MyThread extends Thread { @Override public void run() { try { synchronized (Lock.lock) { System.out.println("before wait: " + Thread.currentThread().getState()); // RUNNABLE Lock.lock.wait(); System.out.println("after wait: " + Thread.currentThread().getState()); // RUNNABLE } } catch (InterruptedException e) { e.printStackTrace(); } } } static class MyThreadB extends Thread { @Override public void run() { synchronized (Lock.lock) { Lock.lock.notify(); } } } } /* before wait: RUNNABLE In main thread state: WAITING after wait: RUNNABLE */