第一章:并发编程线程基础
1.1 什么是线程?
线程是操作系统能够进行运算调度的最小单位,被包含在进程之中,是进程中的实际运作单位。
思考1:线程与进程的区别?
进程是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,一个进程通常包含多个线程,进程之间的线程共享进程的资源。
思考2:并发与并行的区别?
并行是指两个或者多个事件在同一时刻发生;而并发是指两个或多个事件在同一时间间隔发生。
并行(parallel):指在同一时刻,有多条指令在多个处理器上同时执行。所以无论从微观还是从宏观来看,二者都是一起执行的。 
并发(concurrency):指在同一时刻只能有一条指令执行,但多个进程指令被快速的轮换执行,使得在宏观上具有多个进程同时执行的效果,但在微观上并不是同时执行的,只是把时间分成若干段,使多个进程快速交替的执行。

1.2 线程的创建与运行
Java种有两种线程创建的方法,分别是实现Runnable接口的run方法,继承Thread类并重写run方法。

1.2.1 线程创建方式一
继承Thread接口,要求如下:
1)编写程序,开启一个线程,改线程每隔1秒,在控制台输出,”hello world”
2)对上题进行改进:当输出50次”hello world”,结束改线程
3)使用JConsole监控线程执行情况,并画出程序示意图!
*** @Author 周海权* @Date 2022/4/5 9:32* @Version 1.0*/public class ThreadTest{//重写方法快捷键Ctrl+Ostatic class MyThread extends Thread{@Overridepublic void run() {while (true) {try {System.out.println("hello world");Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}public static void main(String[] args) {//创建线程MyThread thread = new MyThread();thread.start();}}
static class MyThread extends Thread{int time = 0;@Overridepublic void run() {while (time<=50) {try {System.out.println("hello world");Thread.sleep(1000);time++;} catch (InterruptedException e) {e.printStackTrace();}}}}
使用JConsole监控线程执行情况:
线程运行的示意图:

思考:为什么是调用start方法而不是直接调用run方法呢?
如果调用run方法,那就相当于没有开启线程去,而是主线程直接调用,那就相当于是串行执行了,而不是并发执行,做一个实现来证明这点。
static class MyThread extends Thread{int time = 0;@Overridepublic void run() {while (time<=10) {try {System.out.println("hello world:"+Thread.currentThread().getName()+"线程调用的");Thread.sleep(1000);time++;} catch (InterruptedException e) {e.printStackTrace();}}}}public static void main(String[] args) throws InterruptedException {//创建线程MyThread thread = new MyThread();thread.run();for (int i = 0 ; i < 10 ; i++){Thread.sleep(1000);System.out.println("主线程i="+i);}}

可以debug源码验证调用Start方法到底为我们做了什么
1.当调用Start的时候立即进入,核心的就是调用Start0()方法
public synchronized void start() {/*** This method is not invoked for the main method thread or "system"* group threads created/set up by the VM. Any new functionality added* to this method in the future may have to also be added to the VM.** A zero status value corresponds to state "NEW".*/if (threadStatus != 0)throw new IllegalThreadStateException();/* Notify the group that this thread is about to be started* so that it can be added to the group's list of threads* and the group's unstarted count can be decremented. */group.add(this);boolean started = false;try {start0();started = true;} finally {try {if (!started) {group.threadStartFailed(this);}} catch (Throwable ignore) {/* do nothing. If start0 threw a Throwable thenit will be passed up the call stack */}}}
2.Start0()是个本地方法,由我们的JVM去调用
private native void start0();/*** If this thread was constructed using a separate* <code>Runnable</code> run object, then that* <code>Runnable</code> object's <code>run</code> method is called;* otherwise, this method does nothing and returns.* <p>* Subclasses of <code>Thread</code> should override this method.** @see #start()* @see #stop()* @see #Thread(ThreadGroup, Runnable, String)*/@Overridepublic void run() {if (target != null) {target.run();}}

总结:其实调用start方法后线程并没有马上执行,而是处于就绪状态,这个就绪状态时指该线程处了CPU之外的资源全部都有了,等待获取CPU资源后才会真正处于运行状态。一旦run方法执行完毕,该线程就处于终止状态。
1.1.2 线程创建方式二:实现Runnable接口
注:因为java是单继承的,如果我们的某个类已经继承了某个父类,如果在继承一个类,就不太可能了,所以我们可以实现Runnable接口
public static void main(String[] args) {RunnableTask runnableTask = new RunnableTask();new Thread(runnableTask).start();}static class RunnableTask implements Runnable{@Overridepublic void run() {System.out.println("hello world: "+Thread.currentThread().getName()+"调用的");}}

问题:为什么可以new Thread(runnableTask)呢?
因为其底层使用了静态代理模式:
用代码模拟Runnable接口如何使用代理模式的
1.3 线程通知与等待
1.3.1 wait()函数
当一个线程调用了wait()方法时,该调用线程会被阻塞挂起,直到发生下面几件事情之一才会返回,
1)其他线程调用了该共享对象的notify()或者notifyAll()方法
2)其他线程调用了该线程的interrupt方法,该线程抛出InterruptedException异常返回
注:如果调用wait()方法的线程必须实现获取该对象的监视器锁。
思考:一个线程如何才能获取一个共享变量的监视器锁呢?
1.执行Synchronized同步代码块,使用该共享变量作为参数
Synchronized(共享变量){
dosomething
}
2.调用该共享变量的方法,并且该方法使用了Sybchronized修饰。
Synchronized void add(int a, int b){
//dosomething
}
1.3.2 notify()函数
一个线程调用共享对象的notify()方法后,会唤醒一个在该共享变量上调用wait系统方法被挂起的线程。一个共享变量上可能会有多个线程在等待具体唤醒哪个线程是随机的。
1.3.3 notifyAll()函数
不同于在共享变量上调用notify()函数会唤醒被阻塞到该共享变量上的一个线程,notifyAll()方法则会唤醒所有在该共享变量上由于调用wait系统方法而被挂起的线程。
1.3.4 通知与等待的代码演示
final static Object ob = new Object();static class T1 extends Thread {@Overridepublic void run() {synchronized (ob) {System.out.println("t1-----我正在等待执行中");try {Thread.sleep(2000);ob.wait();System.out.println("t1------我要开始了");Thread.sleep(3000);System.out.println("t1-------我执行结束了,唤醒t2");ob.notify();} catch (InterruptedException e) {e.printStackTrace();}}}}static class T2 extends Thread {@Overridepublic void run() {synchronized (ob) {System.out.println("t2-----我现在执行了");try {Thread.sleep(1000);System.out.println("t2-----------我释放了执行权");ob.notify();System.out.println("t2-----------我再次拿到了执行权,开始执行");Thread.sleep(5000);System.out.println("t2-----------我等着t1执行之后在执行");ob.wait();System.out.println("t2-----------我彻底结束了");} catch (InterruptedException e) {e.printStackTrace();}}}}public static void main(String[] args) {Thread t1 = new T1();Thread t2 = new T2();t1.start();t2.start();}}
t1先执行,然后阻塞此线程,等待唤醒
t2获取ob 开始执行,唤醒等待的线程中的一个,其他线程必须等待它执行结束之后才可以执行
t1再次拿到执行权,执行
1.4 等待线程执行终止的join方法
在项目实践中经常会遇到一个场景,就是需要等待某几件事完成后才能继续往下执行,比如多个线程加载资源,需要等待多个线程全部加载完毕再汇总处理。
简单的例子如下:
public static void main(String[] args) throws InterruptedException {Thread threadone = new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("child ThreadOne over!");}});Thread threadtwo = new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("child threadTwo over!");}});//启动子线程threadone.start();threadtwo.start();System.out.println("wait all child thread over");//等待子进程执行完毕,返回threadone.join();threadtwo.join();System.out.println("all child thread over");}

1.5 让线程睡眠的sleep方法
Thread类中有一个静态的sleep方法。当一个执行中的线程调用Thread的sleep方法后,调用线程会暂停指定实践的执行权,也就是在这期间不参与CPU的调度,但是该线程所拥有的监视器资源,比如锁还是持不放开、指定的睡眠时间到了后,线程就进入就绪状态,然后参与CPU的调度,
