什么是JUC
- 在 Java 中,线程部分是一个重点,本篇文章说的 JUC 也是关于线程的。JUC
就是 java.util .concurrent 工具包的简称。这是一个处理线程的工具包,JDK
1.5 开始出现的
进程与线程
- 进程(Process) 是计算机中的程序关于某数据集合上的一次运行活动,是系
统进行资源分配和调度的基本单位,是操作系统结构的基础。 在当代面向线程
设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的
描述,进程是程序的实体。是计算机中的程序关于某数据集合上的一次运行活
动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。程序是
指令、数据及其组织形式的描述,进程是程序的实体
- 线程(thread) 是操作系统能够进行运算调度的最小单位。它被包含在进程之
中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,
一个进程中可以并发多个线程,每条线程并行执行不同的任务
- 总结来说:
- 进程:指在系统中正在运行的一个应用程序;程序一旦运行就是进程;进程—
—资源分配的最小单位。
- 线程:系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个
单元执行流。线程——程序执行的最小单位。
线程的状态
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; }
- wait/sleep 的区别
- sleep 是 Thread 的静态方法,wait 是 Object 的方法,任何对象实例都
能调用。
- sleep 不会释放锁,它也不需要占用锁。wait 会释放锁,但调用它的前提
是当前线程占有锁(即代码要在 synchronized 中)。
并发与并行
- 串行模式
- 并行模式
- 并行意味着可以同时取得多个任务,并同时去执行所取得的这些任务。并行模
式相当于将长长的一条队列,划分成了多条短队列,所以并行缩短了任务队列
的长度。并行的效率从代码层次上强依赖于多进程/多线程代码,从硬件角度上
则依赖于多核 CPU
- 并发模式
- 并发(concurrent)指的是多个程序可以同时运行的现象,更细化的是多进程可
以同时运行或者多指令可以同时运行
- 并发:同一时刻多个线程在访问同一个资源,多个线程对一个点
- 并行:多项工作一起执行,之后再汇总
管程
- 管程(monitor)是保证了同一时刻只有一个进程在管程内活动,即管程内定义的操作在同
一时刻只被一个进程调用(由编译器实现).但是这样并不能保证进程以设计的顺序执行
- JVM 中同步是基于进入和退出管程(monitor)对象实现的,每个对象都会有一个管程
(monitor)对象,管程(monitor)会随着 java 对象一同创建和销毁
- 执行线程首先要持有管程对象,然后才能执行方法,当方法完成之后会释放管程,方
法在执行时候会持有管程,其他线程无法再获取同一个管程
用户线程和守护线程
- 用户线程:平时用到的普通线程
- 自定义线程
守护线程:运行在后台,是一种特殊的线程,比如垃圾回收
- 当主线程结束后,用户线程还在运行,JVM 存活
- 如果没有用户线程,都是守护线程,JVM 结束