公平对抗UnFair

时间:2012-12-17 17:09:32

标签: java multithreading

我通过RentrantLock测试了公平和非公平的纪律。我写了一个模拟餐饮哲学家的小程序。

每个philospher都有左右叉,ReentrantLock s。我已经模拟了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())纪律没有太大差异。

(第一个数字是philospher的数字)

公平锁定:

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

我曾预料到一些不公平锁定的饥饿,我的意思是一些哲学家/哲学家必须吃得比其他人大得多,但饥饿并没有发生。为什么呢?

3 个答案:

答案 0 :(得分:21)

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

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

但如果我把锁定公平,我会看到

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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