0
点赞
收藏
分享

微信扫一扫

thread dump日志文件分析

朱小落 2022-05-18 阅读 20


public class TestWait {
public static void main(String[] args) {

Thread thread = new Thread("线程1") {
//重写run方法
public void run() {
synchronized (this) {
System.out.println(Thread.currentThread().getName());
try {
wait();
System.out.println("what the fuck");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (thread) {
System.out.println(Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.notify();
}
}
}

上面代码会先执行线程1 run()方法,然后调用wait()方法阻塞block住。等到主线程调用thread.notify()方法之后才会继续往下执行。




举报

相关推荐

0 条评论