J.U.C:线程基本概念

进程与线程

进程(Process) 是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。 在当代面向线程设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的描述,进程是程序的实体。是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。程序是指令、数据及其组织形式的描述,进程是程序的实体。
线程(thread) 是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。

线程的状态

/**
     * A thread state.  A thread can be in one of the following states:
     * 
    *
  • {@link #NEW}
    * A thread that has not yet started is in this state. *
  • *
  • {@link #RUNNABLE}
    * A thread executing in the Java virtual machine is in this state. *
  • *
  • {@link #BLOCKED}
    * A thread that is blocked waiting for a monitor lock * is in this state. *
  • *
  • {@link #WAITING}
    * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. *
  • *
  • {@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. *
  • *
  • {@link #TERMINATED}
    * A thread that has exited is in this state. *
  • *
* *

* 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: *

    *
  • {@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. */ 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}
  • *
*/
TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }

线程有NEW(新建)、RUNNABLE(就绪)、BLOCKED(阻塞)、WAITING(不见不散)、TIMED_WAITING(过时不候)、TERMINATED(终止)这六种状态。

NEW

表示线程处于新建状态,还不可以运行

RUNNABLE

表示线程处于就绪状态,可以被调度器调度运行,调用线程的start方法后进入该状态。

BLOCKED

表示线程在等待同步监视器锁,一般是在竞争syncronized的锁或者进入syncronized之后调用了Object.wait方法

WAITING

表示线程处于等待状态,一般是调用了以下三个方法之一:

Object#wait()不带参的

相应地需要对应监视器对象调用notify或者notifyAll方法或者被中断interrupted

Thread.join()不带参的

相应地需要另外的线程执行完成

LockSupport#park()方法

相应地需要调用LockSupport#unpark()方法

TIMED_WAITING

表示线程处于带时等待状态,一般是调用了以下五个方法之一:

Thread.sleep(long)

需要等待线程休眠时间到或者被中断interrupted

Object#wait(long)

相应地需要对应监视器对象调用notify或者notifyAll方法或者被中断interrupted

Thread.join(long)

相应地需要另外的线程执行完成

LockSupport#parkNanos

相应地需要调用LockSupport#unpark()方法

LockSupport#parkUntil

相应地需要调用LockSupport#unpark()方法

TERMINATED

线程的终止状态

wait和sleep

  • sleep 是 Thread 的静态方法,wait 是 Object 的方法,任何对象实例都能调用。
  • sleep 不会释放锁,它也不需要占用锁。wait 会释放锁,但调用它的前提是当前线程占有锁(即代码要在 synchronized 中)。
  • 它们都可以被 interrupted 方法中断。

并行与并发

并行是表示同时做多件事情,对于单核CPU来说,是达不到并行的效果的。并行标示的是同时做do多件事的能力。
并发是看起来是同时在做多件事情,对于单核CPU来说,同一时刻只可能一个线程在运行。并发表示的是同时处理handle多件事情的能力。
所以,只有多核CPU才能真正利用好多线程能力。

用户线程和后台线程

用户线程:也叫前台线程,平时用到的普通线程,自定义线程setDaemon(false),默认就是前台线程
守护线程:运行在后台,是一种特殊的线程,setDaemon(true),比如垃圾回收

  • 当主线程结束后,用户线程还在运行,JVM 存活
  • 如果没有用户线程,都是守护线程,JVM 结束
    这里的一个很好的利用就是:如何优雅地保证程序不退出?

你可能感兴趣的:(JavaSE,多线程,并发库,java)