一言不合翻源码,一下就是Thread类源码中State内置枚举,列举出了Java线程的几种状态:
/** * A thread state. A thread can be in one of the following states: * <ul> * <li>{@link #NEW}<br> * A thread that has not yet started is in this state. * </li> * <li>{@link #RUNNABLE}<br> * A thread executing in the Java virtual machine is in this state. * </li> * <li>{@link #BLOCKED}<br> * A thread that is blocked waiting for a monitor lock * is in this state. * </li> * <li>{@link #WAITING}<br> * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. * </li> * <li>{@link #TIMED_WAITING}<br> * A thread that is waiting for another thread to perform an action * for up to a specified waiting time is in this state. * </li> * <li>{@link #TERMINATED}<br> * A thread that has exited is in this state. * </li> * </ul> * * <p> * A thread can be in only one state at a given point in time. * These states are virtual machine states which do not reflect * any operating system thread states. * * @since 1.5 * @see #getState */ public enum State { /** * Thread state for a thread which has not yet started. */ 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. */ 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}. */ 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. */ 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> */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }
1 什么是线程[来源:百度百科]
线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes),但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。
线程是独立调度和分派的基本单位。线程可以为操作系统内核调度的内核线程,如Win32线程;由用户进程自行调度的用户线程,如Linux平台的POSIX Thread;或者由内核与用户进程,如Windows 7的线程,进行混合调度。
同一进程中的多条线程将共享该进程中的全部系统资源,如虚拟地址空间,文件描述符和信号处理等等。但同一进程中的多个线程有各自的调用栈(call stack),自己的寄存器环境(register context),自己的线程本地存储(thread-local storage)。
一个进程可以有很多线程,每条线程并行执行不同的任务。
在多核或多CPU,或支持Hyper-threading的CPU上使用多线程程序设计的好处是显而易见,即提高了程序的执行吞吐率。在单CPU单核的计算机上,使用多线程技术,也可以把进程中负责I/O处理、人机交互而常被阻塞的部分与密集计算的部分分开来执行,编写专门的workhorse线程执行密集计算,从而提高了程序的执行效率。
2 Thread的几种状态
在给定的时间点,线程只能处于一种状态。 这些状态是不反映任何操作系统线程状态的虚拟机状态。
NEW(新线程态)、RUNNABLE(可运行态)、 BLOCKED(阻塞态)、WAITING(等待状态)、TIMED_WAITING(定时等待状态)、TERMINATED(终止状态)
3 分别介绍
3.1 NEW
尚未启动的线程的线程状态。
产生一个Thread对象就生成一个新线程。当线程处于"新线程"状态时,仅仅是一个空线程对象,它还没有分配到系统资源。因此只能启动或终止它。任何其他操作都会引发异常。例如,一个线程调用了new方法之后,并在调用start方法之前的处于新线程状态,可以调用start和stop方法。
触发方式:
Thread thread = new Thread();
3.2 RUNNABLE
可运行线程的线程状态。 一个处于可运行状态的线程正在Java虚拟机中执行,但它可能正在等待来自操作系统(如处理器)的其他资源。
start()方法产生运行线程所必须的资源,调度线程执行,并且调用线程的run()方法。在这时线程处于可运行态。该状态不称为运行态是因为这时的线程并不总是一直占用处理机。特别是对于只有一个处理机的PC而言,任何时刻只能有一个处于可运行态的线程占用处理 机。Java通过调度来实现多线程对处理机的共享。注意,如果线程处于Runnable状态,它也有可能不在运行,这是因为还有优先级和调度问题。
触发方式:
Thread thread = new Thread(); thread.start(); 或 thread.run();
start()和run()方法有什么不同?
再次翻源码:
/** * Causes this thread to begin execution; the Java Virtual Machine * calls the <code>run</code> method of this thread. * <p> * 翻译:使此线程开始执行;Java虚拟机调用该线程的run方法。 * The result is that two threads are running concurrently: the * current thread (which returns from the call to the * <code>start</code> method) and the other thread (which executes its * <code>run</code> method). * 翻译:结果是两个线程同时运行:当前线程(从调用start方法返回)和另一个线程(执行其run方法)。 * <p> * It is never legal to start a thread more than once. * In particular, a thread may not be restarted once it has completed * execution. * 翻译:线程启动一次以上是不合法的。 特别是,线程在完成执行后可能不会重新启动。 */ public synchronized void start() { if (threadStatus != 0) throw new IllegalThreadStateException(); /* 通知该组该线程即将启动,以便将其添加到该组的线程列表中,并且该组的未启动计数可以减少。 */ group.add(this); boolean started = false; try { start0(); started = true; } finally { try { if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */ } } } /** * If this thread was constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object's <code>run</code> method is called; * otherwise, this method does nothing and returns. * 翻译:如果这个线程是使用一个单独的Runnable 对象构造的,那么Runnable对象的run方法被调用;否则,此方法不执 * 行任何操作并返回。 * <p> * Subclasses of <code>Thread</code> should override this method. * 翻译:Thread的子类应该覆盖这个方法。 */ @Override public void run() { if (target != null) { target.run(); } }
由源码我们可以知道:
- 调用start()方法时,先放入线程组,进行异步执行,而不一定是直接进行执行
- 调用run()方法,进行同步执行,即直接执行
3.3 BLOCKED
等待监视器锁而阻塞的线程的线程状态。 处于阻塞状态的线程正在等待监视器锁进入一个同步的块/方法,或者在调用Object.wait之后重新进入一个同步的块/方法。
触发机制:
Object.wait(); thread.interrupt()
3.4 WAITING
一个处于等待状态的线程正在等待另一个线程执行一个特定的动作。
触发机制:
Object.wait(); 或 thread.join() 或(锁支持) LockSupport.park()
取消等待:
Object.notify() 或 Object.notifyAll()
3.5 TIMED_WAITING
具有指定等待时间的等待线程的线程状态。由于调用了下列方法中的一个,并且指定了正等待时间,线程处于定时等待状态
触发机制:
Thread.sleep() 或 Object.wait() 或 Thread.join() 或 LockSupport.parkNanos 或 LockSupport.parkUntil
3.6 TERMINATED
终止线程的线程状态。 线程已经完成执行。
触发机制:
thread.stop();