并发编程之多线程概念
进程和线程
说到多线程,必然绕不开进程与线程的概念及区别。
对于计算机而言,当前的操作系统可以同时运行多个应用程序,而每一个应用程序就是一个进程(Process),它拥有自己独立的内存空间。线程是进程中的一个执行流程,是执行任务的最小单位。一个进程内部允许同时启动多个线程(Thread),同时执行多个任务。线程是轻量级的进程,它负责在单个程序里执行多任务,通常由操作系统负责多个线程的调度和执行。
- /**
- * A thread state. A thread can be in one of the following states:
- * <ul>
- * <li>{@link #NEW}
- * A thread that has not yet started is in this state.
- * </li>
- * <li>{@link #RUNNABLE}
- * A thread executing in the Java virtual machine is in this state.
- *</li>
- * <li>{@link #BLOCKED}
- * A thread that is blocked waiting for a monitor lock
- * is in this state.
- * </li>
- * <li>{@link #WAITING}
- * A thread that is waiting indefinitely for another thread to
- * perform a particular action is in this state.
- * </li>
- * <li>{@link #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.
- * </li>
- * <li>{@link #TERMINATED}
- * 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 static State toThreadState(int var0) {
- if ((var0 & 4) != 0) {
- return State.RUNNABLE;
- } else if ((var0 & 1024) != 0) {
- return State.BLOCKED;
- } else if ((var0 & 16) != 0) {
- return State.WAITING;
- } else if ((var0 & 32) != 0) {
- return State.TIMED_WAITING;
- } else if ((var0 & 2) != 0) {
- return State.TERMINATED;
- } else {
- return (var0 & 1) == 0 ? State.NEW : State.RUNNABLE;
- }
- }
线程状态之间的转换关系如下图:
-
刚创建的线程处于NEW状态
-
调用start()后,线程处于RUNNANLE状态【线程何时开始执行任务,受CPU的控制。当CPU有空闲资源时,才会运行线程】
-
在等待获取锁资源的时候,线程会处于BLOCKED状态
-
线程在等待另一个线程的通知时,会处于WAITING状态
-
线程在指定时间内等待另一个线程通知,会处于TIMED_WAITING状态
-
线程运行结束或出现异常,就会变为TERMINATED状态
-
获取线程的当前状态:getState()
- public Thread.State getState() {
- return VM.toThreadState(this.threadStatus);
- }
从源码中可以看出,线程的状态与VM有很大的关系。在JVM中,与线程创建、运行、销毁等关系比较大的是Java虚拟机栈内存。【JVM的基本机构可查看JVM之基本结构 】