代码拉取完成,页面将自动刷新
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();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。