首页 > 解决方案 > 如何在单车道桥问题上实现 FCFS 算法?

问题描述

如何在单车道桥问题上实现 FCFS 算法?

我正在研究单车道桥问题,并希望避免挨饿。

每当村民开始等待时,我想将他添加到等待队列的末尾,并且每当桥空闲时,我想将村民放在等待队列的顶部。

这是我的代码片段,无需担心饥饿且未实现 FIFO:

            System.out.println("NorthBound Villager "+ count + " is waiting");
            try
            {
                Thread.sleep(2);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            bridge.crossBridge();
            System.out.println("NorthBound Villager " + count + " is crossing the bridge");
            try {
                Thread.sleep(2);
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.println("NorthBound Villager " + count + "left the bridge");
            bridge.left();

    }
}

标签: javaoperating-systemdeadlocksemaphorestarvation

解决方案


推荐阅读