Java线程的状态
2015-09-24 15:53
585 查看
线程一般会有多种状态,Java的状态有:
- NEW
- RUNNABLE
- BLOCKED
- WAITING
- TIMED_WAITING
- TERMINATED
对于各个状态的含义,直接看Thread.java中的源码注释:
这几种状态,可以写程序测试一下:
输出:
state: NEW
输出:
state: NEW
state: RUNNABLE
thread 1
生产者-消费者模型中,生产者速度没有消费者快导致没有足够可消费物品时,消费者就会等待:
输出:
state: WAITING
输出:
state: TIMED_WAITING
输出:
thread1: RUNNABLE
thread2: BLOCKED
输出:
thread 1 running
thread1 :TERMINATED
- NEW
- RUNNABLE
- BLOCKED
- WAITING
- TIMED_WAITING
- TERMINATED
对于各个状态的含义,直接看Thread.java中的源码注释:
public enum State { /** * Thread state for a thread which has not yet started. * 还没有启动的线程状态,比如只定义了一个线程,没有调用strat()方法 */ NEW, /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. * 现在在JVM中正在执行,但是需要等待其他资源比如处理器资源,这时候所处的状态 */ RUNNABLE, /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. * 线程等待监视器锁的状态,线程可能等待进入同步块/方法时或者调用Object.wait() * 方法之后重入一个同步块/方法中状态。 */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. * 线程等待状态。可能因为调用Object.wait没有超时,Thread.join没有超时, * LockSupport.park。 * 线程等待状态是正在等待另一个线程执行特定操作。 */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> * 线程等待特定的时间。调用了sleep,或者带超时的Object.wait,带超时的join, * LockSupport的parkNanos或parkUntil等 */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. * 结束的线程 */ TERMINATED; }
这几种状态,可以写程序测试一下:
Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("thread 1"); } }); System.out.println("state: "+thread1.getState());
输出:
state: NEW
Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("thread 1"); } }); System.out.println("state: "+thread1.getState());
thread1.start();
System.out.println("state: "+thread1.getState());
输出:
state: NEW
state: RUNNABLE
thread 1
生产者-消费者模型中,生产者速度没有消费者快导致没有足够可消费物品时,消费者就会等待:
final Object lock = new Object(); Thread t1 = new Thread() { @Override public void run() { int i = 0; while (true) { synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { } System.out.println(i++); } } } }; t1.start(); System.out.println("state: " + t1.getState());
输出:
state: WAITING
final Object lock = new Object(); Thread t1 = new Thread() { @Override public void run() { int i = 0; while (true) { synchronized (lock) { try { lock.wait(20 * 1000L); } catch (InterruptedException e) { } System.out.println(i++); } } } }; t1.start(); Thread.sleep(1000); System.out.println("state: " + t1.getState());
输出:
state: TIMED_WAITING
public class BlockTest { final Object lock = new Object(); public BlockTest() { } public void test(){ synchronized(lock){ while(true){ } } } public void run() throws InterruptedException{ Runnable r = new Runnable() { @Override public void run() { test(); } }; Thread t1 = new Thread(r); Thread t2 = new Thread(r); t1.start(); Thread.sleep(1000); t2.start(); System.out.println("thread1: "+t1.getState()); System.out.println("thread2: "+t2.getState()); } }
输出:
thread1: RUNNABLE
thread2: BLOCKED
Thread t1 = new Thread(new Runnable() { @Override public void run() { System.out.println("thread 1 running"); } }); t1.start(); Thread.sleep(1000); System.out.println("thread1 :"+t1.getState());
输出:
thread 1 running
thread1 :TERMINATED
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统