一、Object
1.1、wait方法
- 阻塞线程一定时间,释放锁,如果不设置参数,则一直阻塞,直到被notify/notifyAll唤醒或者被interrupt中断(不阻塞,继续执行,但执行中断逻辑)
- 被唤醒时,需要重新竞争锁且竞争成功才能继续往下执行
- 需要先获得锁,才有能释放锁,所以需要在同步块中才能使用
1.2、notify方法
- 随机唤醒一个因调用wait阻塞的线程
- 不会释放锁,所以假设notify后,同步块还有很多耗时操作,那阻塞线程不会立刻被唤醒,它竞争不到锁,无法执行wait之后的语句
- 需要先获得锁,才能去唤醒等待锁的线程,所以需要在同步块中使用
1.3、notifyAll方法
- 唤醒全部因调用wait阻塞的线程
- 不会释放锁
- 需要先获得锁,才能去唤醒等待锁的线程,所以需要在同步块中使用
二、Thread
2.1、sleep方法
- 是一个Thread类的静态方法(因为当前线程一定是运行中的)
- 不需要获得锁,不需要在同步块中使用,不会释放线程拥有的锁
- 阻塞当前线程一定时间,可以被interrupt中断
2.2、join方法
- thread.join() 表示主线程等待thread线程执行结束后再接着执行,其本质是通过Object类的wait、notifyAll实现,因此,会释放锁(锁的对象是thread)
- 不需要在同步块中,因为join方法是同步方法
- 可以被interrupt中断
2.3、yield方法
- 是一个Thread类的静态方法(因为当前线程一定是运行中的)
- 让出cpu时间片,使得当前线程从运行态进入就绪态
三、应用实例
private static final Object obj = new Object();
@Test
public void test() {
System.out.println("...执行主线程...");
Thread thread1 = new Thread(() -> {
System.out.println("进入子线程1中...");
boolean flag = true;
while (flag) {
try {
System.out.println("当前时间..." + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
synchronized (obj) {
obj.wait();
System.out.println("线程1已经重新获得锁");
}
} catch (InterruptedException e) {
flag = false;
System.out.println("线程1被中断....");
}
}
System.out.println("退出子线程1中...");
});
thread1.start();
for (int i = 0; i < 5; i++) {
synchronized (obj) {
obj.notify();
try {
System.out.println("阻塞线程,且不释放锁,看线程1是否能立刻获得锁");
Thread.sleep(3000);
System.out.println("线程1可获得锁");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
thread1.interrupt();
Thread thread2 = new Thread(() -> {
System.out.println("子线程2开始...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("子线程2结束...");
});
thread2.start();
try {
System.out.println("等待子线程2执行结束...");
thread2.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("...主线程结束...");
}
四、为什么wait、notify方法不设计在Thread类里?
- wait方法会阻塞线程,会释放锁,在唤醒时是去随机唤醒因为调用wait阻塞且释放锁的线程
- 由上理解,其实设计在Thread类里也是可行的
- 但是,wait、notify方法是在同步块里才能使用的啊,而同步锁的对象不一定是当前线程对象啊
- 因此不能设计在Thread类里,而Object是所有类的父类,解铃还需系铃人,所以设计在Object类里