public class MyTest {
public static void main(String[] args) {
Object object = new Object();
new Thread(new Runnable() {
@Override
public void run() {
while(true){
synchronized (object){
System.out.println("A");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(true){
synchronized (object){
System.out.println("B");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
}
用多线程交替打印ABABABAB
最新推荐文章于 2024-11-03 11:01:20 发布