11. Concurrency Flashcards
What does the java.util.concurrent.atomic package enable?
The java.util.concurrent.atomic package enables multithreaded applications to safely access
individual variables without locking,
What is an atomic operation?
An atomic operation is one that, for all intents and purposes appears to happen all at once.
Which classes does the atomic package provide?
The atomic package provides several classes for different data types, such as AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference to name a few.
Where does CAS stand for?
CAS stands for Compare And Swap
Wat does CAS do?
- The value is stored in count is copied to a temporary variable
- The temporary variable is incremented
- Compare the value currently in count with the original value. If it is unchanged, then swap
the old value for the new value.
Step 3 happens atomically, if step 3 find that some other thread has already modified the value of count, then repeat step 1-3 until we increment the field without interference.
Which method does provides the CAS behaviour?
The central method in a class like AtomicInteger is the boolean compareAndSet(int expect, int update) method which provides the CAS behavior
What does the lock package provide?
- The ability to duplicate traditional synchronized blocks
- Nonblock scoped locking – obtain a lock in one method and release it in another
- Multiple wait/notify/notifyAll pools per lock – threads can select which pool they wait on
- The ability to attempt to acquire a lock and take an alternative action if locking fails
- An implementation of a multiple-reader, single-writer lock
Why choose a lock above a synchronization?
The reason for you to choose to use a Lock instance instead of a traditional synchronization is that one of the very powerful features is the ability to attempt to acquire a lock. You can process a different resource (lock) and come back to the failed lock later instead of just waiting for a lock to be released and thereby making more efficient use of system resources.
What does the tryLock() method do?
The tryLock() method attempts to lock the Lock instance immediately. It returns true if the locking succeeds, false if Lock is already locked. This method never blocks.
When does a IllegalMonitorStateException get thrown?
If a thread attempts to release a lock that it does not own, an
IllegalMonitorStateException will be thrown.
What is a reentrantReadWriteLock?
A reentrantReadWriteLock is not actually a Lock; it implements the ReadWriteLock interface. What a ReentrantReadWriteLock does is produce two specialized Lock instances, one to a read lock and the other to a write lock. What makes these locks unique is that multiple threads can hold the read lock at the same time, but only one thread can hold the write lock at a time.
Which concurrent collections contains the concurrent package?
- ConcurrentHashMap
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentSkipListMap
- ConcurrentSkipListSet
What is a blocking queue?
A BlockingQueue is a type of shared collection that is used to exchange data between 2 or more threads while causing one or more of the threads to wait until the point in time when the data can be exchanged.
What is the producer-consumer scenario?
In a producer-consumer scenario, one thread produces data, then adds it to the queue, and another thread must consume the data from the queue.
What are the BlockingQueue implementations?
- ArrayBlockingQueue
- LinkedBlockingDeQue
- LinkedBlockingQueue
- PriorityBlockingQueue
- DelayQueue
- LinkedTransferQueue
- SynchronousQueue