1 进程与线程的概念
1.1 进程和线程
进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。在早期面向进程设计的计算机结构中,进程是程序的基本执行实体;在当代面向线程设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的描述,进程是程序的实体。
线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。
总结来说:
进程:指在系统中正在运行的一个应用程序;程序一旦运行就是进程;进程是资源分配的最小单位
线程:系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个单元执行流。线程时程序执行的最小单位
1.2 线程的状态
进入 java.lang.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;
}
可以看出NEW(新建)、RUNNABLE(准备就绪)、BLOCKED(阻塞)、WAITING(等待-不见不散)、
TIMED_WAITING(等待-过时不候)、TERMINATED(终结)
1.3 wait和sleep
- sleep是Thread的静态方法;wait是Object的方法,任何对象实例都能调用。
- sleep不会释放锁,它也不需要占用锁;wait会释放锁,但调用它的前提是当前线程占有锁(即代码要在synchronized中)
- 它们都可以被interrupt方法中断
1.4 并发与并行
并发:同一时间间隔内多个线程正在执行,实际上是宏观上并行,微观上串行
并行:同一时刻多个线程正在执行
1.5 管程
保证了同一时刻只有一个进程在管程内活动,即管程内定义的操作在同一时刻只被一个进程调用(由编译器实现)。
1.6 用户线程和守护线程
用户线程:自定义线程
主线程结束了,用户线程还在运行,jvm还存活
守护线程:比如说垃圾回收线程
没有用户线程了,只有守护线程,jvm结束
public class MyThread {
public static void main(String[] args) {
//使用Lambda 表达式实现这个接口,创建 线程t1
Thread t1 = new Thread(() -> {
//判断是否是守护线程,(后台运行的)
System.out.println(Thread.currentThread().getName() + "::" + Thread.currentThread().isDaemon());
while (true) {
//主线程结束,程序还在运行,jvm 没停止
}
}, "t1");
// 把他设置为守护线程 ,主线程结束这个程序没有用户线程了,结束了
t1.setDaemon(false);
//启动线程
t1.start();
System.out.println(Thread.currentThread().getName() +"结束");
}
}
输出结果为:
main结束
t1::false