探寻俄罗斯数字腹地:Yandex,不止于搜索的巨匠
记录一道常用的线程调度算法题实现如下public class PrintABC {private static final int MAX_PRINT_COUNT 10; // 循环打印次数private static int state 0; // 线程状态0表示打印A1表示打印B2表示打印Cpublic static void main(String[] args) {Object lock new Object();Thread threadA new Thread(() - {for (int i 0; i MAX_PRINT_COUNT; i) {synchronized (lock) {while (state % 3 !
{ // 轮到A打印try {lock.wait();} catch (InterruptedException e) {Thread.currentThread().interrupt();}}System.out.print(A);state;lock.notifyAll();}}});Thread threadB new Thread(() - {for (int i 0; i MAX_PRINT_COUNT; i) {synchronized (lock) {while (state % 3 !
{ // 轮到B打印try {lock.wait();} catch (InterruptedException e) {Thread.currentThread().interrupt();}}System.out.print(B);state;lock.notifyAll();}}});Thread threadC new Thread(() - {for (int i 0; i MAX_PRINT_COUNT; i) {synchronized (lock) {while (state % 3 !
{ // 轮到C打印try {lock.wait();} catch (InterruptedException e) {Thread.currentThread().interrupt();}}System.out.print(C);state;lock.notifyAll();}}});// 启动三个线程threadA.start();threadB.start();threadC.start();}}
伊人APP-伊人应用