Java并发编程学习笔记(一)
线程8大核心基础知识(一)
1.实现多线程的方法有几种?
方法一:实现 Runnable 接口(推荐)
/**
* 描述: 用Runnable方式创建线程
*/
public class RunnableStyle implements Runnable{
public static void main(String[] args) {
Thread thread = new Thread(new RunnableStyle());
thread.start();
}
@Override
public void run() {
System.out.println("用Runnable方法实现线程");
}
}
方法二:基础 Thread 类
/**
* 描述: 用Thread方式实现线程
*/
public class ThreadStyle extends Thread{
@Override
public void run() {
System.out.println("用Thread类实现线程");
}
public static void main(String[] args) {
new ThreadStyle().start();
}
}
如果同时使用以上两种方法,将执行 Thread 方式的,会将 Runnable 类的核心 run()方法重写覆盖掉。
/**
* 描述: 同时使用Runnable和Thread两种实现线程的方式
*/
public class BothRunnableThread {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("我来自Runnable");
}
}) {
@Override
public void run() {
System.out.println("我来自Thread");
}
}.start();
}
}
• 分析
• 从面向对象的思想去考虑
- 首先创建了一个匿名内部类 Thread。传入了一个 Runnable 对象。
- 然后重写了 Thread 的 run 方法。最后启动线程。
- 因为重写了 Thread 的 run 方法,所以它父类的 run 方法就被覆盖掉了,所以即便传入了 Runnable 对象也不会执行它。
总结:
按Oracle官方来说,通常可以分为两类。
但准确的来讲,创建线程只有一种方式,那就是构造 Thread 类,而实现线程的执行单元有两种方式。
方法一:实现 Runnable 接口的 run() ,并把 Runnable 实例传给 Thread 类。
方法二:重写 Thread 的 run(),即继承 Thread 类。
追加:线程池的本质也是 new Thread,故不是一种新的创建方法。定时器、匿名类以及 lambda 等也是(需从源码实现分析)。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 线程池创建线程的方法
*/
public class ThreadPools {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 1000; i++) {
// 添加任务
executorService.submit(new Task() {});
}
}
}
class Task implements Runnable {
@Override
public void run() {
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
线程池创建线程源码
问题:实现 Runable 接口和继承 Thread 类哪种方式好?
答案是实现 Runable 接口更好。
使用 Runnable 方式的好处:
1):适合多个相同的程序代码的线程去处理同一个资源,把虚拟CPU(线程)同程序的代码、数据有效分离,较好地体现了面向对象的设计思想;
2):可以避免java中的单继承的限制;
3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。当多个线程的执行代码来自同一个类的实例时,继承它们共享相同的代码。多个线程可以操作相同的数据,与他们的代码无关当共享访问相同的对象时即共享相同的数据。当线程被构造时,需要的代码和数据通过一个对象作为构造函数实参传递进去,这个对象就是一个实现了Runnable接口的类的实例。
使用Thread方法的缺点:
ⅰ. 从代码的架构去考虑,具体执行的任务也就是 run 方法中的内容,它应该和线程的创建、运行的机制也就是 Thread 类是解耦的。所以不应该把他们混为一谈。从解耦的角度,方法一更好。
ⅱ. 该方法每次如果想新建一个任务,只能去新建一个独立的线程,而新建一个独立的线程这样的损耗是比较大的,它需要去创建、然后执行,执行完了还要销毁;而如果使用 Runnable 接口的方式,我们就可以利用线程池之类的工具,利用这些工具就可以大大减小这些创建线程、销毁线程所带来的损耗。所以方法一相比于方法二的这一点,好在资源的节约上。
ⅲ. 继承了 Thread 类之后,由于 Java 不支持双继承,那么这个类就无法继承其他的类了,这大大限制了我们的可扩展性。
• 两种方式的本质区别
• 方法一( Runnable 接口): 最终调用 target.run; ,通过以下两图可以知道使用这个方法时实际上是传递了一个 target 对象,执行了这个对象的 run 方法。
方法二(Thread类): run() 整个都被重写。一旦子类重写了父类的方法,原有方法会被覆盖被抛弃,即以下代码不会被这次调用所采纳。
Thread继承了Runnbale接口。
综上,两种方法都是执行了 run 方法,只不过 run 方法的来源不同。
2.怎样才是正确的线程启动方式?
start() 和 run() 的比较
start() 方法含义:真正意义上启动一个线程,才能经历线程的各个生命周期。
PS: 不能重复 start(),在start()运行最开始就会判断是否有被调用过。
源码解析:
->启动新线程检查线程状态
->加入线程组
->调用 start0()
/**
* 描述: 对比start和run两种启动线程的方式
*/
public class StartAndRunMethod {
public static void main(String[] args) {
Runnable runnable = () -> {
System.out.println(Thread.currentThread().getName());
};
runnable.run();
new Thread(runnable).start();
}
}
总结:
start 方法会调用start0方法,start0是native方法,会启动一个线程,由虚拟机去调用线程的run方法,这里由于重写了父类的run方法,所以调用子类的run方法。
run 方法是直接在主线程里执行的,没有创建线程,只是调用了对象的方法而已,因为没有调用start0本地方法
3.如何正确停止线程?
原理介绍:使用 interrupt 来通知,而不是强制停止。
线程会停止的情况:run() 方法运行完;出现异常。
/**
* 描述: run方法内没有sleep或wait方法时,停止线程
*/
public class RightWayStopThreadWithoutSleep implements Runnable {
@Override
public void run() {
int num = 0;
//注意这里需要判断线程状态是否中断
while (!Thread.currentThread().isInterrupted() && num <= Integer.MAX_VALUE / 2) {
if (num % 10000 == 0) {
System.out.println(num + "是10000的倍数");
}
num++;
}
System.out.println("任务运行结束了");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
thread.start();
Thread.sleep(2000);
thread.interrupt();
}
}
阻塞情况下停止线程
即在运行中使用 sleep():采用 try-catch 保证程序正常运行
/**
* 描述: 带有sleep的中断线程的写法
*/
public class RightWayStopThreadWithSleep {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = () -> {
int num = 0;
try {
while (num <= 300 && !Thread.currentThread().isInterrupted()) {
if (num % 100 == 0) {
System.out.println(num + "是100的倍数");
}
num++;
}
//线程中带有 sleep(),而不是在 start() 后才有 sleep()
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(500);
thread.interrupt();
}
}
每次迭代都阻塞(每次循环都有sleep、wati等)
注意比起前两种,不再需要在 while 中 判断 Thread 的状态
/**
* 描述: 如果在执行过程中,每次循环都会调用sleep或wait等方法,那么不需要每次迭代都检查是否已中断
*/
public class RightWayStopThreadWithSleepEveryLoop {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = () -> {
int num = 0;
try {
while (num <= 10000) {
if (num % 100 == 0) {
System.out.println(num + "是100的倍数");
}
num++;
//在循环内 sleep()
Thread.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
实际开发中的两种最佳实践
优先选择:传递中断
run() 无法抛出 checked Exception ,只能用 try/catch,是顶层方法。
/**
* 描述: 最佳实践:catch了InterruptedExcetion之后的优先选择:
* 在方法签名中抛出异常 那么在run()就会强制try/catch
*/
public class RightWayStopThreadInProd implements Runnable {
@Override
public void run() {
while (true && !Thread.currentThread().isInterrupted()) {
System.out.println("go");
try {
throwInMethod();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
//保存日志、停止程序
System.out.println("保存日志");
e.printStackTrace();
}
}
}
private void throwInMethod() throws InterruptedException {
Thread.sleep(2000);
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadInProd());
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
不想或无法传递:恢复中断
/**
* 描述:最佳实践2:在catch子语句中调用Thread.currentThread().interrupt()来恢复设置中断状态,
* 以便于在后续的执行中,依然能够检查到刚才发生了中断
* 回到刚才RightWayStopThreadInProd补上中断,让它跳出
*/
public class RightWayStopThreadInProd2 implements Runnable {
@Override
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("Interrupted,程序运行结束");
break;
}
reInterrupt();
}
}
private void reInterrupt() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadInProd2());
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
切记:不应屏蔽中断
追加:错误的停止方法
1.被弃用的 stop, suspend, resume 方法
->会导致程序运行一半突然停止,没办法完成一个基本单位的操作,造成脏数据。
2.用 volatile 设置 boolean 标记位
->陷入阻塞时,volatile是无法中断线程的 。此例中,生产者的生产速度很快,消费者消费速度慢,所以阻塞队列(BlockingQueue)满了以后,生产者会阻塞,等待消费者进一步消费
4.线程的生命周期——6个状态
New
Runnable
Blocked
Waiting
Timed_Waiting
Terminated
各状态间的转化:
阻塞状态:
一般习惯而言,把 Blocked(被阻塞)、Waiting(等待)、Timed_waiting(计时等待)都称为阻塞状态。不仅仅指 Blocked。
常见问题
线程有哪几种状态?声明周期是什么?
要点:先回答六个状态,再结合关系图上 单向/双向 转化的关系,以及通过调用什么方法进行转化
5.Thread和Object类中和线程相关的重要方法
1.方法概览
2.wait, notify, notifyAll 方法详解
作用:阻塞阶段、唤醒阶段、遇到中断
在阻塞阶段中,直到以下 4 种情况之一发生时,才会被唤醒
1)另一个线程调用这个对象的 notify() 方法且刚好被唤醒的是本线程;
2)另一个线程调用这个对象的 notifyAll() 方法;
3)过了 wait(long timeout) 规定的超时时间,如果传入 0 就是永久等待;
4)线程自身调用了 interrupt() 。
使用 wait() 会释放锁 - - 当前锁
/**
* 描述: 展示wait和notify的基本用法 1. 研究代码执行顺序 2. 证明wait释放锁
*/
public class Wait {
public static Object object = new Object();
static class Thread1 extends Thread {
@Override
public void run() {
synchronized (object) {
System.out.println(Thread.currentThread().getName() + "开始执行了");
try {
//释放锁,等待被再次唤醒才继续执行未完成的代码
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程" + Thread.currentThread().getName() + "获取到了锁。");
}
}
}
static class Thread2 extends Thread {
@Override
public void run() {
synchronized (object) {
//唤醒其他线程,执行完代码后将锁让出去
object.notify();
System.out.println("线程" + Thread.currentThread().getName() + "调用了notify()");
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
thread1.start();
Thread.sleep(200);
thread2.start();
}
}
结果
Thread-0开始执行了
线程Thread-1调用了notify()
线程Thread-0获取到了锁。
start() 先执行,不代表线程先启动
/**
* 描述: 3个线程,线程1和线程2首先被阻塞,线程3唤醒它们。notify, notifyAll。 start先执行不代表线程先启动。
*/
public class WaitNotifyAll implements Runnable {
private static final Object resourceA = new Object();
public static void main(String[] args) throws InterruptedException {
Runnable r = new WaitNotifyAll();
Thread threadA = new Thread(r);
Thread threadB = new Thread(r);
Thread threadC = new Thread(new Runnable() {
//threadC自己的 run() 方法,用来唤醒 A,B线程
@Override
public void run() {
synchronized (resourceA) {
//threadC 得到锁会唤醒其余所有线程,执行完代码就把锁让回去
resourceA.notifyAll();
// resourceA.notify();
System.out.println("ThreadC notified.");
}
}
});
threadA.start();
threadB.start();
// Thread.sleep(200);
threadC.start();
}
@Override
public void run() {
synchronized (resourceA) {
System.out.println(Thread.currentThread().getName()+" got resourceA lock.");
try {
System.out.println(Thread.currentThread().getName()+" waits to start.");
//将锁释放掉,等待唤醒,锁让给其他线程
resourceA.wait();
//直到被再次唤醒才会打印信息
System.out.println(Thread.currentThread().getName()+"'s waiting to end.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
结果
Thread-0 got resourceA lock.
Thread-0 waits to start.
Thread-1 got resourceA lock.
Thread-1 waits to start.
ThreadC notified.
Thread-1's waiting to end.
Thread-0's waiting to end.
wait, notify, notifyAll 特点、性质
1)必须先拥有 monitor
2)notify() 只能唤醒其中一个
3)都属于 Object 类
4)类似功能的 Condition (JDK 封装好了)
5)同持有多个锁的情况
为什么 wait() 需要在同步代码块内使用,而 sleep() 不需要?
答:为了让通信变得可靠,防止死锁或永久等待
为什么线程通信的方法 wait(),notify(),notifyAll() 被定义在 Object 类里?而 sleep() 定义在 Thread 类里?(重要,仔细阅读)
JAVA提供的锁是对象级的而不是线程级的,每个对象都有个锁,而线程是可以获得这个对象的。因此线程需要等待某些锁,那么只要调用对象中的wait()方法便可以了。而wait()方法如果定义在Thread类中的话,那么线程正在等待的是哪个锁就不明确了。这也就是说wait,notify和notifyAll都是锁级别的操作,所以把他们定义在Object类中是因为锁是属于对象的原因。
sleep的作用是:让线程在预期的时间内执行,其他时候不要来占用CPU资源。从上面的话术中,便可以理解为sleep是属于线程级别的,它是为了让线程在限定的时间后去执行。而且sleep方法是不会去释放锁的。
Wait-notify机制是在获取对象锁的前提下不同线程间的通信机制。在Java中,任意对象都可以当作锁来使用,由于锁对象的任意性,所以这些通信方法需要被定义在Object类里。
简单概括:
1)在java的内置锁机制中,每个对象都可以成为锁,也就是说每个对象都可以去调用wait,notify方法,而Object类是所有类的一个父类,把这些方法放在Object中,则java中的所有对象都可以去调用这些方法了。
2)一个线程可以拥有多个对象锁,wait,notify,notifyAll跟对象锁之间是有一个绑定关系的,比如你用对象锁Object调用的wait()方法,那么你只能通过Object.notify()或者Object.notifyAll()来唤醒这个线程,这样jvm很容易就知道应该从哪个对象锁的等待池中去唤醒线程,假如用Thread.wait(),Thread.notify(),Thread.notifyAll()来调用,虚拟机根本就不知道需要操作的对象锁是哪一个。
追加1:wait() 是属于 Object 对象的,那如果调用 Thread.wait() 会怎么样?
答:自动 notify(),对操作流程造成影响。
追加2:norifyAll 之后所有的线程都会再次抢夺锁,若某线程抢夺失败会怎么样?
答:回到初始状态,继续等待锁的释放,进入下一轮抢夺锁。
追加3:用 suspend() 和 resume() 来阻塞线程可以吗?
答:弃用了,采用 wait & notify.
3.sleep 方法详解
作用:让线程在预期的时间执行,其他时候不要占用 CPU 资源。期间不释放锁- - -包括synchronized 和 lock,和 wait 不同(会将锁释放)。
/**
* 展示线程sleep的时候不释放synchronized的monitor,等sleep时间到了以后,正常结束后才释放锁
*/
public class SleepDontReleaseMonitor implements Runnable {
public static void main(String[] args) {
SleepDontReleaseMonitor sleepDontReleaseMonitor = new SleepDontReleaseMonitor();
new Thread(sleepDontReleaseMonitor).start();
new Thread(sleepDontReleaseMonitor).start();
}
@Override
public void run() {
syn();
}
private synchronized void syn() {
System.out.println("线程" + Thread.currentThread().getName() + "获取到了monitor。");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程" + Thread.currentThread().getName() + "退出了同步代码块");
}
}
/**
* 描述: 演示sleep不释放lock(lock需要手动释放)
*/
public class SleepDontReleaseLock implements Runnable {
private static final Lock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
System.out.println("线程" + Thread.currentThread().getName() + "获取到了锁");
try {
Thread.sleep(5000);
System.out.println("线程" + Thread.currentThread().getName() + "已经苏醒");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
SleepDontReleaseLock sleepDontReleaseLock = new SleepDontReleaseLock();
new Thread(sleepDontReleaseLock).start();
new Thread(sleepDontReleaseLock).start();
}
}
sleep 方法响应中断
1)抛出 InterruptedException
2)清除中断状态:Thread.sleep();或 TimeUnit.SECONDS.sleep();更优雅
/**
* 描述: 每个1秒钟输出当前时间,被中断,观察。
* Thread.sleep()
* TimeUnit.SECONDS.sleep()
*/
public class SleepInterrupted implements Runnable{
//抛出异常
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new SleepInterrupted());
thread.start();
Thread.sleep(6500);
thread.interrupt();
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(new Date());
try {
//清除中断状态
TimeUnit.HOURS.sleep(3);
TimeUnit.MINUTES.sleep(25);
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("我被中断了!");
e.printStackTrace();
}
}
}
}
总结
sleep 方法可以让线程进入 Waiting 状态,并且不占用 CPU 资源,但是不释放锁,直到规定时间后再执行,休眠期间如果被中断,会抛出异常并清除中断状态。
4.join 方法
作用:因为新的线程加入了我们,所以我们要等他执行完再出发(保证线程没执行完之前不做其他操作)
用法:main 等待 thread1 执行完毕,注意是 main 等待 thread1 的执行
普通用法
/**
* 描述: 演示join,注意语句输出顺序,会变化。
*/
public class Join {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "执行完毕");
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "执行完毕");
}
});
thread.start();
thread2.start();
System.out.println("开始等待子线程运行完毕");
thread.join();
thread2.join();
//只有当所有线程运行完毕,才打印信息
System.out.println("所有子线程执行完毕");
}
}
遇到中断
实际上是 主线程被中断(本来就是 main 等待 thread),如果不添加中断子线程,子线程仍会继续运行,所以需要在 try-catch 中主动中断子线程。
/**
* 描述: 演示join期间被中断的效果
*/
public class JoinInterrupt {
public static void main(String[] args) {
Thread mainThread = Thread.currentThread();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
//中断主线程,子线程并不会被中断
mainThread.interrupt();
Thread.sleep(5000);
System.out.println("Thread1 finished.");
} catch (InterruptedException e) {
System.out.println("子线程中断");
}
}
});
thread1.start();
System.out.println("等待子线程运行完毕");
try {
thread1.join();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"主线程中断了");
//主动中断子线程
thread1.interrupt();
}
System.out.println("子线程已运行完毕");
}
}
join 期间,主线程的状态是:Waiting
(验证:先 join,通过 mainThread.getState()查看/debug)
追加:join 的等价代码
/**
* 描述: 通过讲解join原理,分析出join的代替写法
*/
public class JoinPrinciple {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "执行完毕");
}
});
thread.start();
System.out.println("开始等待子线程运行完毕");
thread.join();
//等待其他代码运行完唤醒,join的等价代码如下
// synchronized (thread) {
// thread.wait();
// }
System.out.println("所有子线程执行完毕");
}
}
5.yield 方法
作用:释放我的 CPU 时间片(释放完仍然是 runnable)
yield 和 sleep 的区别:是否可以随时再次被调度(yield 可以)
6.获取当前执行线程的引用:Thread.currentThread()
/**
* 描述: 演示打印main, Thread-0, Thread-1
*/
public class CurrentThread implements Runnable {
public static void main(String[] args) {
new CurrentThread().run();
new Thread(new CurrentThread()).start();
new Thread(new CurrentThread()).start();
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
7.start 和 run 方法
8.stop, suspend, resume 方法,已弃用