Python 官方文档:入门教程 => 点击学习
目录一、sleep和wait方法的区别二、wait方法wait方法的使用wait结束等待的条件三、notify和notifyAll方法一、sleep和wait方法的区别 根本区别:s
wait方法
public class WaitDemo1 {
public static void main(String[] args) {
Object lock = new Object();
Thread t1 = new Thread(() -> {
System.out.println("线程1开始执行");
try {
synchronized (lock) {
System.out.println("线程1调用wait方法....");
// 无限期的等待状态
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1执行完成");
}, "线程1");
t1.start();
}
}
有参wait线程和无参wait线程
public class WaitDemo2 {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();
Thread t1 = new Thread(()->{
System.out.println("线程1开始执行");
synchronized (lock1){
try {
lock1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1执行完成");
}
},"无参wait线程");
t1.start();
Thread t2 = new Thread(()->{
System.out.println("线程2开始执行");
synchronized (lock2){
try {
lock2.wait(60*60*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程2执行完成");
}
},"有参wait线程");
t2.start();
}
}
①其他线程调用该对象的 notify 方法.
②wait 等待时间超时 (wait 方法提供一个带有 timeout 参数的版本, 来指定等待时间).
③其他线程调用该等待线程的 interrupted 方法, 导致 wait 抛出 InterruptedException 异常
notify 方法只是唤醒某一个等待的线程
notify方法的使用
public class WaitNotifyDemo {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();
Thread t1 = new Thread(()->{
System.out.println("线程1开始执行");
try {
synchronized (lock1) {
System.out.println("线程1调用wait方法");
lock1.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1执行完成");
},"线程1");
Thread t2 = new Thread(()->{
System.out.println("线程2开始执行");
try {
synchronized (lock1) {
System.out.println("线程2调用wait方法");
lock1.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程2执行完成");
},"线程2");
t1.start();
t2.start();
// 唤醒 lock1 对象上休眠的线程的(随机唤醒一个)
Thread t3 = new Thread(()->{
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程3开始执行");
synchronized (lock1){
//发出唤醒通知
System.out.println("执行了唤醒");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"线程3");
t3.start();
}
}
notifyAll方法可以一次唤醒所有的等待线程
notifyAll方法的使用
public class WaitNotifyAll {
public static void main(String[] args) {
Object lock = new Object();
new Thread(() -> {
System.out.println("线程1:开始执行");
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1:执行完成");
}
}, "无参wait线程").start();
new Thread(() -> {
synchronized (lock) {
System.out.println("线程2:开始执行 |" + LocalDateTime.now());
try {
lock.wait(60 * 60 * 60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程2:执行完成 | " + LocalDateTime.now());
}
}, "有参wait线程").start();
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock) {
System.out.println("唤醒所有线程");
lock.notifyAll();
}
}).start();
}
}
notify和notifyAll方法的区别
到此这篇关于Java详细分析sleep和wait方法有哪些区别的文章就介绍到这了,更多相关Java sleep与wait内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java详细分析sleep和wait方法有哪些区别
本文链接: https://lsjlt.com/news/147344.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0