1. 概念
进程(Process)
- 计算机中的程序关于某数据集合上的一次运行活动
- 进程是线程的容器
- 程序是指令、数据及其组织形式的描述,进程是程序的实体
- 系统进行资源分配和调度的基本单位,是操作系统结构的基础
线程(thread)
- 操作系统能够进行运算调度的最小单位
- 被包含在进程之中,是进程中的实际运作单位
- 一条线程指的是进程中一个单一顺序的控制流
通俗易懂理解
- 进程代表一个程序的执行,而线程是程序中的某个单元执行流
- 程序一旦运行就是进程;进程是资源分配的最小单位而线程是程序执行的最小单位。一个进程中可以并发多个线程,每条线程并行执行不同的任务
举个列子:打开一个程序,就是进程, 而某个进程中有多个功能(一个功能代表一个线程)
2. 线程
通过线程枚举类的状态了解具体的操作
创建(new)、就绪(runnable)、运行(running)、阻塞(blocked)、time waiting、waiting、消亡(dead)
查看源代码Thread.State
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;(终结) }
2.2 wait和sleep的区别
| 区别 | wait | sleep |
|---|---|---|
| 不同点 | Object 的方法,会释放锁,调用它的前提是当前线程占有锁(即代码要在 synchronized 中) | Thread 的静态方法,任何对象实例都能调用。不会释放锁,也不需要占用锁 |
| 相同点 | 都可以被 interrupted 方法中断 | 都可以被 interrupted 方法中断 |
2.3 用户线程和守护线程
用户线程是自定义的线程
守护线程是后台的特殊线程,依赖于创建它的线程,比如jvm的垃圾回收
在main中定义一个守护线程,main结束,守护线程就会结束,而用户线程不会。
主线程如果结束了用户线程还在执行,代表jvm存活
Thread a = new Thread(() -> {// Thread.currentThread().isDaemon() 是否是守护线程: true表示是System.out.println(Thread.currentThread().getName() + "::" + Thread.currentThread().isDaemon());while (true) {}}, "a");a.start();System.out.println(Thread.currentThread().getName()+"over");
打印的值为:false,表示是用户线程
- 没有用户线程后,普遍都是守护线程代表jvm结束
Thread a = new Thread(() -> {// Thread.currentThread().isDaemon() 是否是守护线程: true表示是System.out.println(Thread.currentThread().getName() + "::" + Thread.currentThread().isDaemon());while (true) {}}, "a");// 设置a为守护线程。当主线程结束后,如果只存在守护线程,则jvm会直接退出。因为用户线程都已经执行完毕了。守护线程一般不参与业务处理。a.setDaemon(true);a.start();System.out.println(Thread.currentThread().getName()+"over");
- 用户线程包括守护线程
2.4 多线程编程
多线程的编程步骤主要是:(高内聚低耦合)
- 创建资源类,在资源类中船舰属性和操作方法
- 创建多个线程,调用资源类的操作方法
3. 串行、并行和并发
串行表示所有任务都一一按先后顺序进行
并行同时取得多个任务,并同时去执行所取得的这些任务
并发(concurrent):多个程序可以同时运行也是多进程可以同时运行或者多指令可以同时运行
通俗易懂的理解
- 并发是同一时刻多个线程在访问同一个资源
- 并行是多项工作一起执行,之后再汇总
举例比如:多个人访问同个网址,为多个线程访问一个网址,多对一此为并发。而并行是多个工作一起执行,每个工作都是一个资源一个线程。之后合并起来就是一个并行工作
结合以上的搭配可以产生多路复用或异步的方式
实际上,对于单核心 CPU 来说,同一时刻只能运行一个线程。所以,这里的”同时运行”表示的不是真的同一时刻有多个线程运行的现象,这是并行的概念,而是提供一种功能让用户看来多个程序同时运行起来了,但实际上这些程序中的进程不是一直霸占 CPU 的,而是执行一会停一会
4. 管程
管程在java中是锁,在操作系统中是monitor监视器
代表一种同步机制,同一时间内只能有一个线程访问且被保护数据
比如jvm的同步基于进入和退出,是管程对象实现
- 每个对象都有一个monitor管程对象,都会随着java的对象进行创建和销毁
- 管程对象对临界区加锁和解锁
大意就是进加锁,退是解锁,通过管程对象管理
