public class Test2 {
public static void main(String[] args) {
String lock = "lock";
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("线程1开始等待" + System.currentTimeMillis());
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1结束等待" + System.currentTimeMillis());
}
}
}){
};
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
System.out.println("线程2开始等待唤醒" + System.currentTimeMillis());
lock.notify();
System.out.println("线程2开始唤醒" + System.currentTimeMillis());
}
}
}){
};
thread1.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
}
wait和notify使用例子
原创
©著作权归作者所有:来自51CTO博客作者wx60bc17759a517的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
聊聊TCP中的TIME_WAIT
TIME_WAIT 的产生、作用以及优化
TIME_WAIT