 |
|
YoongRii
V2EX member #322421, joined on 2018-06-13 12:32:05 +08:00
|
 |
Per YoongRii's settings, the topics list is hidden |
Deals info, including closed deals, is not hidden
YoongRii's recent replies
用原子变量%3 这种解法比较常见,但是需要线程忙等,提供一种用线程池方式实现的思路:
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService1 = Executors.newSingleThreadExecutor();
ExecutorService executorService2 = Executors.newSingleThreadExecutor();
ExecutorService executorService3 = Executors.newSingleThreadExecutor();
Runnable[] rs = new Runnable[3];
rs[0] = () -> {
System.out.println("A");
executorService2.submit(rs[1]);
};
rs[1] = () -> {
System.out.println("B");
executorService2.submit(rs[2]);
};
rs[2] = new Runnable() {
private int a = 1;
public void run() {
System.out.println("C");
if (a++ < 100)
executorService3.submit(rs[0]);
}
};
executorService1.submit(rs[0]);
}