你可以使用 synchronized
和 wait
/ notify
来实现多线程交替打印abc。
以下是一个示例代码:
public class PrintABC {
private intstate;
public synchronized void printA() throws InterruptedException {
while (state % 3 != 0) {
wait();
}
System.out.print("A");
state++;