12

我已经通过RentrantLock. 我写了一个模拟吃饭哲学家的小程序。

每个philospher都有左右叉,它们是ReentrantLocks。我已经模拟了 1000 次思考和进食的行为:

 for (int i = 0; i < ACT_TIMES; i++) {
            act();
        }

act在哪里

private void act() {
        think();
        eat();

    }

Think没意思,它只是睡了一段时间。这是eat方法

private void eat() {
        try {
            if (left.tryLock(0, TimeUnit.MILLISECONDS)) {
                if (right.tryLock(0, TimeUnit.MILLISECONDS)) {
                    log("eating");
                    eatCount++;
                    try {
                        Thread.sleep(EAT_TIME);
                    } catch (InterruptedException e) {
                    } finally {
                        left.unlock();
                        right.unlock();
                    }
                } else {
                    left.unlock();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

主要方法:

 Lock[] forks = new Lock[5];
        for (int i = 0; i < forks.length; i++) {
            forks[i] = new ReentrantLock();
        }
        Philosopher p1 = new Philosopher(0, forks[1], forks[0]);
        Philosopher p2 = new Philosopher(1, forks[2], forks[1]);
        Philosopher p3 = new Philosopher(2, forks[3], forks[2]);
        Philosopher p4 = new Philosopher(3, forks[4], forks[3]);
        Philosopher p5 = new Philosopher(4, forks[0], forks[4]);
        ExecutorService exec = Executors.newFixedThreadPool(5);
        exec.submit(p1);
        exec.submit(p2);
        exec.submit(p3);
        exec.submit(p4);
        exec.submit(p5);

在所有 5 个线程完成后,我为每个哲学家打印eatCount。new ReentrantLock(true)而且这些值对于公平( )和不公平(new ReentrantLock())纪律并没有太大差异。

(第一个数字是哲学家的数字)

公平锁:

0 344
1 348
2 366
3 359
4 363
Total number of eating 1780

不公平的锁:

0 338
1 338
2 339
3 341
4 352
Total number of eating 1708

我已经预料到不公平锁定会导致饥饿,我的意思是一些哲学家/哲学家的 eatCount 比其他人大得多,但饥饿并没有发生。为什么?

4

3 回答 3

23

释放锁的线程有更好的机会重新获得锁,因为它很忙,而其他线程可能被阻塞。忙等待不会显示这一点,因为每个线程都有相同的机会获取锁。可能释放锁的人可能处于轻微劣势。

final ReentrantLock lock = new ReentrantLock();
for (int i = 0; i < 5; i++) {
    final int finalI = i;
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int j = 0; j < 5; j++) {
                lock.lock();
                System.out.println("locked by " + finalI);
                lock.unlock();
            }
        }
    }).start();
}

印刷

locked by 0
locked by 0
locked by 0
locked by 0
locked by 0
locked by 1
locked by 1
locked by 1
locked by 1
locked by 1
locked by 2
locked by 2
locked by 2
locked by 2
locked by 2
locked by 3
locked by 3
locked by 3
locked by 3
locked by 3
locked by 4
locked by 4
locked by 4
locked by 4
locked by 4

但是如果我用 true 使锁公平,我会看到

locked by 0
locked by 1
locked by 2
locked by 3
locked by 4
locked by 0
locked by 1
locked by 2
locked by 3
locked by 4
locked by 0
locked by 1
locked by 2
locked by 3
locked by 4
locked by 0
locked by 1
locked by 2
locked by 3
locked by 4
locked by 0
locked by 1
locked by 2
locked by 3
locked by 4
于 2012-12-17T17:32:07.653 回答
1

全部删除sleep(),您可能会看到一些不公平。

于 2012-12-17T17:27:18.970 回答
0

@Maks,您是否尝试过为 EAT_TIME 设置随机值?

这可能会给逻辑带来一些不公平。

于 2016-11-30T13:16:18.853 回答