public class Multi {
public static void main(String[] args) throws Exception {
final Object obj = new Object();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
synchronized (obj) {
obj.notifyAll();
obj.wait();
System.out.println(1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
synchronized (obj) {
obj.notifyAll();
obj.wait();
System.out.println(2);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
};
}
多线程面试题——交替打印1和2
最新推荐文章于 2022-01-14 21:31:32 发布