JUC
JUC是java.util .concurrent 工具包的简称,DK1.5出现。这是一个处理线程的工具包,可以实现多线程高并发的内容
进程与线程
进程与线程的概念
- 进程(Process)
- 计算机中的程序关于某数据集合上的一次运行活动
- 进程是线程的容器
- 程序是指令、数据及其组织形式的描述,进程是程序的实体
- 系统进行资源分配和调度的基本单位,是操作系统结构的基础
- 线程(thread)
- 操作系统能够进行运算调度的最小单位
- 被包含在进程之中,是进程中的实际运作单位
一条线程指的是进程中一个单一顺序的控制流
总结来说
进程:指在系统中正在运行的一个应用程序;程序一旦运行就是进程;进程— —资源分配的最小单位。 线程:系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个 单元执行流。线程——程序执行的最小单位。线程的状态
通过线程枚举类的状态了解具体的操作 创建(new)、就绪(runnable)、运行(running)、阻塞(blocked)、time waiting、waiting、消亡(dead)
源码:Thread.State ```java 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}
- Thread state for a terminated thread.
- The thread has completed execution.
*/
TERMINATED;(终结) }
```
两个方法:wait和sleep
- sleep 是 Thread 的静态方法,wait 是 Object 的方法,任何对象实例都 能调用。
- sleep 不会释放锁,它也不需要占用锁。wait 会释放锁,但调用它的前提 是当前线程占有锁(即代码要在 synchronized 中)。
它们都可以被 interrupted 方法中断。 两者都是哪里睡哪里醒
并行与并发
串行
串行表示所有任务都一一按先后顺序进行。 串行是一次只能取得一个任务,并执行这个任务。
并行
并行意味着可以同时取得多个任务,并同时去执行所取得的这些任务。并行模 式相当于将长长的一条队列,划分成了多条短队列,所以并行缩短了任务队列 的长度。并行的效率从代码层次上强依赖于多进程/多线程代码,从硬件角度上 则依赖于多核 CPU。
并发
多个程序可以同时运行也是多进程可以同时运行或者多指令可以同时运行。但是这里的同时运行并非真实的同时运行,而是快速切换cpu执行多线程让用户看起来是多线程在同时运行。并发的重点在于它是一种现象。
要解决大并发问题,通常是将大任务分解成多个小任务, 由于操作系统对进程的 调度是随机的,所以切分成多个小任务后,可能会从任一小任务处执行。
管程
- 管程(monitor)是保证了同一时刻只有一个进程在管程内活动,即管程内定义的操作在同 一时刻只被一个进程调用(由编译器实现).保护数据或代码
- JVM 中同步是基于进入和退出管程(monitor)对象实现的,每个对象都会有一个管程 (monitor)对象,管程(monitor)会随着 java 对象一同创建和销毁
执行线程首先要持有管程对象,然后才能执行方法,当方法完成之后会释放管程,方 法在执行时候会持有管程,其他线程无法再获取同一个管程
用户线程和守护线程
用户线程:平时用到的普通线程,自定义线程
- 守护线程:运行在后台,是一种特殊的线程,比如后台的垃圾回收线程
- 当主线程结束后,用户线程还在运行,JVM 存活 ;如果主线程结束且没有用户线程,都是守护线程,JVM 结束 。jvm的存活与否是看是否仅有守护线程
```java
public static void main(String[] args) {
}Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("新建线程 线程结束...");
}
});
thread.setDaemon(true); //①
thread.start();
System.out.println("主线程 结束...");
//当主线程结束且新建线程并不是守护线程,则jvm依然存活等待新建线程结束 //thread.setDaemon(true); 当主线程结束后新建线程为守护线程,则jvm停止 ```