Our first design, the big lock design, allows only one thread at a time! The following class illustrates this:
public class BigLockHashSet<T> extends HashSet<T> { final Lock lock; final int LIST_LEN_THRESHOLD = 100; public BigLockHashSet(int capacity) { super(capacity); lock = new ReentrantLock(); }
As shown in the preceding code, the class is a subclass of HashSet<T>
and uses a ReentrantLock
. As noted earlier, a reentrant lock is one that allows the owner thread to reacquire it. The LIST_LEN_THRESHOLD
constant is used in theshouldResize()
method that is described in the next section.
The lock()
and unlock()
methods are overridden, and they just ignore the x
parameter. The methods are shown in the following code:
@Override protected void unlock(T x) { lock.unlock(); } @Override protected void lock(T x) { lock.lock(); }
As shown in the preceding code, this implementation just locks and unlocks the reentrant lock; it does not make any use of the element...