Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Concurrent Patterns and Best Practices

You're reading from   Concurrent Patterns and Best Practices Build scalable apps in Java with multithreading, synchronization and functional programming patterns

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher Packt
ISBN-13 9781788627900
Length 264 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Atul S. Khot Atul S. Khot
Author Profile Icon Atul S. Khot
Atul S. Khot
Arrow right icon
View More author details
Toc

The big lock approach 

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 the shouldResize() 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 icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime