public class Test {
private boolean flag = true;
public static void main(String[] args) {
Test vt = new Test();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
vt.printA();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
vt.printB();
}
}
}).start();
}
private synchronized void printA(){
while (!flag){
try{
wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.print("A");
flag = false;
notify();
}
private synchronized void
两个线程实现打印100次AB字符串——腾讯面试
最新推荐文章于 2023-05-23 17:45:14 发布