5、核心五:Thread和Object类中的重要方法
5.1 方法概览
5.2 wait,notify,notifyAll方法详解💟
5.2.1 作用、用法
我们可以通过上面方法控制一些线程去休息或唤醒
当一个线程使用wait方法时,这个线程被阻塞(阻塞阶段)并且释放锁
由阻塞状态变为唤醒阶段有几种情况?
- 另一个线程调用这个对象的notify()方法且刚好被唤醒的是本线程
- 另一个线程调用这个对象的notifyAll()方法
- 过了wait( long timeout )规定的时间,如果传入0就是永久等待
- 线程自身调用了interrupt()
遇到中断会由阻塞状态变为唤醒状态
public static void main (String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run () {
try {
System.out.println("t1 begin sleep 2000 seconds");
Thread.sleep(2000000);
System.out.println("t1 awaking");
} catch (InterruptedException e) {
System.out.println("t1 is interrupted while sleeping");
return;
}
System.out.println("t1 continue working");
}
});
t1.start();
Thread.sleep(1000);
//打断子线程的休眠,让子线程从sleep函数返回
t1.interrupt();
//等待子线程执行完毕
t1.join();
System.out.println("main thread is over");
}
输出
5.2.2 代码演示
wait演示
public class Wait {
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();//释放了锁,并且将线程t1阻塞
} 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 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
Thread.sleep(2000);
t2.start();
}
}
输出
线程Thread-0开始执行
线程Thread-1调用了notify()
线程Thread-0释放了锁
描述一下代码执行的整个过程:
- 线程t1得到了锁object,打印出"线程Thread-0开始执行"
- 线程执行到了object.wait()。锁object被释放,线程t1被阻塞
- 线程t2得到了锁object,执行语句object.notify(),此时线程t1被
- 唤醒,但是该线程不能获得锁object。必须要等t2释放了锁之后才能运行
- t1重新获取到锁之后会返回之前的位置,继续执行代码
notifyAll演示
/**
* 线程1和2首先被阻塞,线程3唤醒它们
*/
public class WaitNotifyAll implements Runnable{
private static final Object resourceA = new Object();
@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()
+"got resourceA lock too.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main (String[] args) throws InterruptedException {
WaitNotifyAll r = new WaitNotifyAll();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(new Runnable() {
@Override
public void run () {
synchronized (resourceA) {
resourceA.notifyAll();
System.out.println("ThreadC notified.");
}
}
});
t1.start();
t2.start();
Thread.sleep(4000);
t3.start();
}
}
输出
Thread-0got resourceA lock.
Thread-0waits to start.
Thread-1got resourceA lock.
Thread-1waits to start.
ThreadC notified.
Thread-0got resourceA lock too.
Thread-1got resourceA lock too.
notify演示
public class WaitNotifyAll implements Runnable{
private static final Object resourceA = new Object();
@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()+"got resourceA lock too.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main (String[] args) throws InterruptedException {
WaitNotifyAll r = new WaitNotifyAll();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(new Runnable() {
@Override
public void run () {
synchronized (resourceA) {
resourceA.notify();
System.out.println("ThreadC notified.");
}
}
});
t1.start();
t2.start();
Thread.sleep(4000);
t3.start();
}
}
输出
证明wait只会释放当前这把锁monitor
/**
* 证明wait只释放当前的那把锁
*/
public class WaitNotifyReleaseOwnMonitor {
private static Object resourceA = new Object();
private static Object resourceB = new Object();
public static void main (String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run () {
synchronized (resourceA) {
System.out.println("ThreadA got resourceA lock.");
synchronized (resourceB) {
System.out.println("ThreadA got resourceB lock.");
try {
System.out.println("ThreadA releases resourceA lock.");
resourceA.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run () {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (resourceA) {
System.out.println("ThreadB got resourceA lock.");
System.out.println("ThreadB try got resourceA lock.");
synchronized (resourceB) {
System.out.println("ThreadB got resourceB lock.");
}
}
}
});
thread1.start();
thread2.start();
}
}
输出
5.2.3 特点、性质
- 执行到 wait , notify , notifyAll 时,我们必须获取到monitor(三个方法的使用大多数是在synchronized代码块中)
- notify方法只会唤醒一个线程,这个被唤醒的线程不是由我们决定的。而是由JVM决定的
- 以上三个方法都是Object类的方法,而且都是 native
- 类似功能的 Condition
- 同时持有多个锁的情况:上面代码thread1同时持有resourceA和resourceB两把锁。wait只能释放当前锁。所以很容易发生死锁
5.2.4 wait原理
要明白途中数字代表的情况
现在我们来补充之前线程六大状态转移图
5.3 生产者消费者—使用wait_notify实现
问题引入:现实生活中生产者消费者之间的速度可能不匹配。其中有可能生产者比较快,也有可能生产者比较慢,可能会出现等待现象。这个时候出现了一个设计模式——生产者消费者模式很好的解决了等待问题。该模式实际上将生产者和消费者进行解耦,提高了两者之间的默契度。该模式通常利用一个容器(通常是阻塞队列)来解决两者之间的耦合问题。两者之间的通信通过容器完成
通信大致如下:
代码演示
/**
* 生产者消费者模式:wait/notify
*/
public class ProducerConsumerModel {
public static void main (String[] args) {
EventStorage eventStorage = new EventStorage();
Producer producer = new Producer(eventStorage);
Consumer consumer = new Consumer(eventStorage);
Thread pro = new Thread(producer);
Thread con = new Thread(consumer);
pro.start();
con.start();
}
}
class Producer implements Runnable{
private EventStorage storage;
public Producer (EventStorage storage) {
this.storage = storage;
}
@Override
public void run () {
for (int i = 0; i < 100; i++) {
storage.put();
}
}
}
class Consumer implements Runnable{
private EventStorage storage;
public Consumer (EventStorage storage) {
this.storage = storage;
}
@Override
public void run () {
for (int i = 0; i < 100; i++) {
storage.take();
}
}
}
class EventStorage{
private int maxSize;
private LinkedList<Date> storage;
public EventStorage () {
}
{
this.maxSize = 10;
this.storage = new LinkedList<>();
}
/**
* 放入
* synchronized修饰默认方法:锁对象默认是this
*/
public synchronized void put(){
while (storage.size() == maxSize){//如果满了等待
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storage.add(new Date());
System.out.println("仓库里有了"+storage.size()+"个产品");
notify();//唤醒沉睡的消费者
}
/**
* 拿出
*/
public synchronized void take(){
while(storage.isEmpty()){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("拿到了"+storage.poll()+",现在仓库还剩下 "+storage.size());
notify();//通知生产者干活了
}
}
输出
仓库里有了1个产品
仓库里有了2个产品
仓库里有了3个产品
仓库里有了4个产品
仓库里有了5个产品
仓库里有了6个产品
仓库里有了7个产品
仓库里有了8个产品
仓库里有了9个产品
仓库里有了10个产品
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 9
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 8
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 7
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 6
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 5
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 4
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 3
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 2
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 1
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 0
...
仓库里有了1个产品
仓库里有了2个产品
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 1
拿到了Fri May 10 14:42:34 CST 2024,现在仓库还剩下 0
仓库里有了1个产品
仓库里有了2个产品
仓库里有了3个产品
仓库里有了4个产品
仓库里有了5个产品
仓库里有了6个产品
仓库里有了7个产品
仓库里有了8个产品
仓库里有了9个产品
仓库里有了10个产品
5.4 面试题
写代码:实现两个线程交替打印0~100的奇偶数
描述:一个线程打印奇数,一个线程打印偶数。它们之间交替打印
思路:使用关键字synchronized
/**
* 实现两个线程交替打印0~100的奇偶数
* 使用synchronized
*/
public class WaitNotifyPrintEvenSyn {
private static int i = 0;
private static Object lock = new Object();
public static void main (String[] args) {
new Thread(new Runnable() {
@Override
public void run () {
while (i < 100) {
synchronized (lock) {
if ((i & 1) == 0) {
System.out.println(Thread.currentThread().getName() + " 打印 " + i);
i++;
}
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run () {
while (i < 100) {
synchronized (lock) {
if ((i & 1) == 1) {
System.out.println(Thread.currentThread().getName() + " 打印 " + i);
i++;
}
}
}
}
}).start();
}
}
思路:使用wait/notify(更好)
/**
* 使用wait/notify
* 1. 拿到锁,我们打印
* 2. 唤醒别人,休眠自己
*/
public class WaitNotifyPrintOddEveWait {
private static int i = 0;
private static Object lock = new Object();
static class TurnningThread implements Runnable{
@Override
public void run () {
while(i<100){
synchronized (lock){
System.out.println(Thread.currentThread().getName()+":"+i++);
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main (String[] args) {
TurnningThread turnningThread = new TurnningThread();
new Thread(turnningThread,"x").start();
new Thread(turnningThread,"y").start();
}
}
注意:上面的代码的逻辑一定要在脑子里面想好
手写生产者消费者设计模式
先说生产者消费者模式的使用场景。然后写代码
为什么wait()需要在同步代码块内使用,而sleep()不需要
答案:主要是为了让通信变得可靠,防止死锁等待的发生。如果我们没有将wait/notify/notifyAll放在同步代码块中,没了同步代码块的保护,就会发生一种现象:在线程快要执行到了wait()
时,线程突然被切换。切换到线程2上面,执行了线程2的notify()
。这样就在wait()
之前执行了notify()
。这样不符合我们设计wait/notify的初衷(wait方法尽量要在notify方法之前执行)。所以设计者考虑上面的问题,将需要多个线程之间的配合的动作(方法)在同步代码块中执行。但是sleep()
针对一个线程的方法,所以不必要在同步代码块中
为什么线程通信的方法wait(), notify(), notifyAll()被定义在Object类中?而sleep定义在Thread类中
答案:在Java规定中wait(), notify(), notifyAll()是锁级别的操作,而锁是属于对象的而不是线程中。如果将wait(), notify(), notifyAll()变为线程级别的操作,因为一个线程可能有多个锁,我们没有办法实现上面这样的灵活的逻辑了
wait方法属于Object对象的,那调用Thread.wait会怎样?
答案:因为wait是锁级别的,这样调用就表示Thread是一个锁。Thread还是一个特殊的存在:当线程退出的时候会自动地执行notify( JVM源码中有)。所以我们调用wait方法或常见锁对象的时候不要使用Thread类
如何选择notify和notifyAll?
唤醒一个线程和唤醒多个问题
调用notifyAll之后所有的线程全部被唤醒,它们会一起抢夺锁,如果某线程抢夺失败怎么办?
失败者会等待重新释放锁,然后再次抢
用suspend()和resume()来阻塞线程可以吗?为什么?
不推荐,推荐使用wait和notify来实现相关功能
5.5 sleep方法详解💟
作用:让线程在预期的时间执行,其他时候不要占用CPU资源(一旦执行了sleep之后,线程处于阻塞状态,不在占用CPU资源。直到下次被调度之后才会使用CPU资源)
应用场景:一个空调的程序需要每分钟检查一下空气温度器,如果温度过高则加大风速
特点:
-
不释放锁
- 包括synchronized和lock
- 和wait不同,他不会谦让锁,她睡觉时也拿着锁不释放
-
sleep可以响应中断
-
抛出InterruptedException
-
清除中断状态
/** * 每隔一秒钟输出当前时间,被中断,观察 * 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.SECONDS.sleep(1); } catch (InterruptedException e) { System.out.println("我被中断了"); e.printStackTrace(); } } } }
输出
Fri May 10 18:44:32 CST 2024 Fri May 10 18:44:33 CST 2024 Fri May 10 18:44:34 CST 2024 Fri May 10 18:44:35 CST 2024 Fri May 10 18:44:36 CST 2024 Fri May 10 18:44:37 CST 2024 Fri May 10 18:44:38 CST 2024 我被中断了 Fri May 10 18:44:38 CST 2024 java.lang.InterruptedException: sleep interrupted Fri May 10 18:44:39 CST 2024 Fri May 10 18:44:40 CST 2024
注意到上面的sleep写法
TimeUnit.SECONDS.sleep()
好处:- 可以指定参数单位HOURS、MINUTES、SECONDS
- 对负数进行异常处理
-
总结:sleep方法可以让线程进入Waiting状态,并且不占用CPU资源,但是不释放锁,直到规定时间后再执行,休眠期间如果被中断,会抛出异常并清除中断状态💟
面试题:
wait/notify和sleep方法的异同?(方法属于哪个对象?线程状态怎么切换?)
相同
- Wait和sleep方法都可以使线程阻塞,对应线程状态是Waiting或Time_Waiting。
- wait和sleep方法都可以响应中断Thread.interrupt()。
不同
- wait方法的执行必须在同步方法中进行,而sleep则不需要。(背后的原因说一下)
- 在同步方法里执行sleep方法时,不会释放monitor锁,但是wait方法会释放monitor锁。
- sleep方法短暂休眠之后会主动退出阻塞,而没有指定时间的 wait方法则需要被其他线程中断后才能退出阻塞。
- wait()和notify(),notifyAll()是Object类的方法,sleep()和yield()是Thread类的方法(背后的原因说一下)
5.6 join方法详解
作用:因为新的线程加入我们,所以我们要等他执行完再出发
用法:main等待thread1执行完毕,注意谁等谁
public class A implements Runnable{
public static void main (String[] args) throws InterruptedException {
Thread thread1 = new Thread(new A());
thread1.join();
}
}
5.6.1 join三个例子
普通用法
/**
* 演示join,注意语句输出顺序,会变化
*/
public class Join {
public static void main (String[] args) throws InterruptedException {
Thread thread1 = 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() + "执行完毕");
}
});
thread1.start();
thread2.start();
System.out.println("开始等待子线程运行完毕");
thread1.join();
thread2.join();
System.out.println("所有子线程执行完毕");
}
}
输出
开始等待子线程运行完毕
Thread-1执行完毕
Thread-0执行完毕
所有子线程执行完毕
现在我们为了表明join方法的重要性,我们注释掉上面代码中的所有的join方法
输出
开始等待子线程运行完毕
所有子线程执行完毕
Thread-1执行完毕
Thread-0执行完毕
遇到中断
public class JoinInterrupt {
public static void main (String[] args) {
Thread main = Thread.currentThread();
Thread t = new Thread(new Runnable() {
@Override
public void run () {
try {
main.interrupt();
Thread.sleep(1000);
System.out.println("thread1 finish.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
System.out.println("等待子线程运行完毕");
try {
t.join();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName());
e.printStackTrace();
}
System.out.println("子线程已经运行完毕");
}
}
输出
主线程被中断了,但是,子线程还没有中断。这里涉及到线程中断的技巧:主线程中断子线程也要中断。
public class JoinInterrupt {
public static void main (String[] args) {
Thread main = Thread.currentThread();
Thread t = new Thread(new Runnable() {
@Override
public void run () {
try {
main.interrupt();
Thread.sleep(1000);
System.out.println("thread1 finish.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
System.out.println("等待子线程运行完毕");
try {
t.join();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"被中断了");
t.interrupt();//处理子线程中断问题
}
System.out.println("子线程已经运行完毕");
}
}
join期间,线程到底是什么状态?Waiting
public class JoinThreadState {
public static void main (String[] args) throws InterruptedException {
Thread main = Thread.currentThread();
Thread thread = new Thread(new Runnable() {
@Override
public void run () {
try {
Thread.sleep(3000);
System.out.println(main.getState());
System.out.println("Thread-0运行结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
System.out.println("等待子线程运行完毕");
thread.join();
System.out.println("子线程运行结束");
}
}
输出
等待子线程运行完毕
WAITING
Thread-0运行结束
子线程运行结束
5.6.2 join注意点
如果业务中出现了一个线程等待另一个线程,我们尽量不要使用join,尽量使用CountDownLatch或CyclicBarrier类。因为这些类更成熟更安全
5.6.3 join原理
public final void join() throws InterruptedException {
join(0);
}
public final synchronized void join(final long millis) throws InterruptedException {
if (millis > 0) {
if (isAlive()) {
final long startTime = System.nanoTime();
long delay = millis;
do {
wait(delay);
} while (isAlive() && (delay =
millis - TimeUnit.NANOSECONDS.toMillis(System.nanoTime() -startTime)) > 0);
}
} else if (millis == 0) {
while (isAlive()) {
wait(0);//参数0:表示它一直是等待直到被唤醒
}
} else {
throw new IllegalArgumentException("timeout value is negative");
}
}
当join方法参数是0时,我们一直休眠。请问谁来进行唤醒操作?
因为每一个Thread类,它在run方法运行结束后会自动地执行notify操作(这就是为什么我们不建议使用Thread类来调用wait方法 )
来看一下wait方法的C++源码:
static void ensure_join(JavaThread* thread){
Handle threadObj(thread,thread->threadObj());
ObjectLocker lock(threadObj,thread);
thread->clear_pending_exception();
java_lang_Thread::set_thread_status(threadObj(),java_lang_Thread::TERMINATED);
java_lang_Thread::set_thread(threadObj(),NULL);
lock.notify_all(thread);//这里执行了notify_all,进行了wait的唤醒
thread->clear_pending_exception();
}
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();
synchronized (thread){
thread.wait();
}
System.out.println("所有子线程执行完毕");
}
}
5.6.4 面试题
在join期间,线程处于哪种状态?
子线程加入到main线程中,main线程处于waiting
5.7 yield方法详解
作用:释放我的CPU时间片
定位:JVM不保证遵循yield的作用,有时不会释放时间片
yield和sleep区别:是否随时可能再次被调度
5.8 Thread.currentThread()
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());
}
}
输出
main
Thread-0
Thread-1
5.9 start和run方法
前面详细介绍过
5.10 stop,suspend,resume方法
已被弃用,不再赘述
5.11 面试常见问题
本章面试问题非常多