0

我的 CHM 已经包含以下数据 ->

1 苹果
2 香蕉
3 猫
4 狗

1,2,3,4 是键,Apple,banana... 是键。

如果 3 个线程 t1, t2, t3 想要修改相同的现有记录 [3 Cat] 。我需要按 FIFO 顺序写入。[然后将执行一些操作。Ex Logging to keep record of value change pattern] 我检查了现有的源代码,它不保证 FIFO 写入。在源代码锁定是在一个段上执行的。以下是 CHM 类中的源代码

   static final class Segment<K,V> extends ReentrantLock implements Serializable {

            Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
                         this.loadFactor = lf;
                         this.threshold = threshold;
                         this.table = tab;
            }
            // .... Rest of Code
    }   

Segment Class 调用不提供公平锁定机制的 ReentrantLock 的 Default 构造函数。

public ReentrantLock() {
    sync = new NonfairSync();
}

如果我编写我的新 CHM 课程并进行以下更改

            Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
                         super(true);
                         this.loadFactor = lf;
                         this.threshold = threshold;
                         this.table = tab;
            }

super(true) 将调用 ReEntrantLock 的以下构造函数

           public ReentrantLock(boolean fair) {
                sync = fair ? new FairSync() : new NonfairSync();
           }

我认为这将使 sagment 锁定操作公平,并且写入操作将按 FIFO 顺序执行。请提出您的意见。

4

0 回答 0