import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class D {
private static volatile int index = 0;
public static Lock lock = new ReentrantLock();
public static void main(String[] args) {
new Thread(() -> {
while (true){
lock.lock();
try {
if (index % 3 == 0) {
index++;
System.out.print("a");
}
}finally {
lock.unlock();
}
}
}).start();
new Thread(() -> {
while (true){
lock.lock();
try {
if (index % 3 == 1) {
index++;
System.out.print("b");
}
}finally {
lock.unlock();
}
}
}).start();
new Thread(() -> {
while (true){
lock.lock();
try {
if (index % 3 == 2) {
index++;
System.out.print("c");
}
}finally {
lock.unlock();
}
}
}).start();
}
}
面试的时候大脑空白什么也没写出来,惨~~~