package com.company.bingfa;
class MLock{
}
class MyThread11 extends Thread{
private MLock lock;
MyThread11(MLock lock){
this.lock = lock;
}
@Override
public void run() {
synchronized (lock) {
System.out.println("开始线程1");
try {
System.out.println("线程1开始等待");
lock.wait();
System.out.println("线程1等待结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("结束线程1");
}
}
}
class MyThread12 extends Thread{
private MLock lock;
MyThread12(MLock lock){
this.lock = lock;
}
@Override
public void run() {
synchronized (lock){
System.out.println("开始线程2");
try {
sleep(2000);
System.out.println("继续线程2");
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.notify();
System.out.println("结束线程2");
}
}
}
public class MyNotify {
public static void main(String[] args) {
MLock lock = new MLock();
new MyThread11(lock).start();
new MyThread12(lock).start();
}
}
wait notify
猜你喜欢
转载自blog.csdn.net/yongwoozzang/article/details/103643646
今日推荐
周排行