多线程操作共享变量顺序输出abc 记一次al面试题

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();

}

}
上一篇:appium踩坑记录:解决每次安装appium setting和Unlock


下一篇:appium+rf 问题六--appium setting和unlock在设备上重复安装