Java线程的状态

线程一般会有多种状态,Java的状态有:
- 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:
         * 
    *
  • {@link Object#wait() Object.wait} with no timeout
  • *
  • {@link #join() Thread.join} with no timeout
  • *
  • {@link LockSupport#park() LockSupport.park}
  • *
* *

A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * 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: *

    *
  • {@link #sleep Thread.sleep}
  • *
  • {@link Object#wait(long) Object.wait} with timeout
  • *
  • {@link #join(long) Thread.join} with timeout
  • *
  • {@link LockSupport#parkNanos LockSupport.parkNanos}
  • *
  • {@link LockSupport#parkUntil LockSupport.parkUntil}
  • *
* 线程等待特定的时间。调用了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

作者:xiangshimoni 发表于2015/9/24 15:53:15 原文链接
阅读:24 评论:0 查看评论

你可能感兴趣的:(java,线程,状态)