1 Star 0 Fork 0

包曼/JavaEE

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Demo2.java 1.11 KB
一键复制 编辑 原始数据 按行查看 历史
包曼 提交于 2024-07-03 15:15 +08:00 . 等待和通知机制
import java.util.Scanner;
/**
* @author Baoman
* @create 2024--06--26--16:36
* 等待与通知
**/
public class Demo2 {
//wait与notify
public static void main(String[] args) throws InterruptedException {
Object locker = new Object();
Thread t1 = new Thread(()-> {
synchronized (locker) {
System.out.println("t1等待之前");
try {
locker.wait(1000*10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("t1等待之后");
}
});
Thread t2 = new Thread(()-> {
synchronized (locker) {
System.out.println("t2通知之前");
Scanner scanner = new Scanner(System.in);
System.out.println("输入数字");
int x = scanner.nextInt();
locker.notify();
System.out.println("t2通知之后");
}
});
t1.start();
Thread.sleep(500);
t2.start();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Bao__man/java-ee.git
git@gitee.com:Bao__man/java-ee.git
Bao__man
java-ee
JavaEE
master

搜索帮助