java.util.concurrent.locks
Interfaces
Lock | Condition | ReadWriteLock
Executor | ExecutorService | CompletionService | ScheduledExecutorService | ThreadFactory
Callable | Future | RunnableFuture | ScheduledFuture | RunnableScheduledFuture |
ForkJoinTask | RecursiveTask | RecursiveAction
Limitations of synchronised
1. Only one thread at a time can acquire the lock and all access to the shared resource requires that the lock be acquired first.
Alternative - ReadWriteLocks (readLock() , writeLock()) - maintains a pair of associated
locks
, one for read-only operations and one for writing.
2. The use of
synchronized
methods or statements provides access to the implicit monitor lock associated with every object, but forces all lock acquisition and release to occur in a block-structured way: when multiple locks are acquired they must be released in the opposite order, and all locks must be released in the same lexical scope in which they were acquired.
Alternative - Lock ( Lock.lock() , Lock.unlock() , tryLock() , newCondition() , lockInterruptibility() )
tryLock() = Acquires the lock only if it is not held by another thread at the time of invocation. Does NOT block.
3. Cant do "hand-over-hand" or "chain locking"
Lock
- lock() - Blocks till Lock acquired
- unlock() - Should be called in finally
- tryLock() - Acquires the lock only if it is free at the time of invocation. Does NOT block. Returns true/false
- lockInterruptibility() - can throw InterruptedException
- newCondition() - bound to this Lock instance
Condition
- await() - Causes the current thread to wait until it is signalled or interrupted.
- awaitUninterruptibly() - Can NOT be interrupted. Does not throw InterruptedException
- awaitUntill() -
false
if the deadline has elapsed upon return, elsetrue
- signal() - Wakes up one waiting thread.
- signalAll() - Wakes up all waiting threads.
A
ReentrantLock
is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock
will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock.
ReentrantReadWriteLock
FairMode - When the currently held lock is released either the longest-waiting single writer thread will be assigned the write lock, or if there is a group of reader threads waiting longer than all waiting writer threads, that group will be assigned the read lock.
Barging - Because checks in acquire are invoked before enqueuing, a newly acquiring thread may barge ahead of others that are blocked and queued.
Reentrancy
Lock downgrading
Interruption of lock acquisition
AbstractQueuedSynchronizer
Callable
Runnable
Future -
RunnableFuture - FutureTask
ForkJoinTask
BlockingQueue
BlockingDeque
TransferQueue
Exchanger
ConcurrentMap - ConcurrentHashMap
Executor
ExecutorService
CompletionService - ExecutorCompletionService
ScheduledExecutorService - ScheduledThreadPoolExecutor
CyclicBarrier - Reusable, Can take Runnable, - await()
CountDownLatch - One time only - await() & countDown()
Semaphore( int permits) - acquire(), release(), tryAcquire()
http://stackoverflow.com/questions/19660737/aba-in-lock-free-algorithms
CopyOnWriteArrayList
{
final transient ReentrantLock lock = new ReentrantLock();
private transient volatile Object[] array;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long lockOffset;
}
AbstractQueuedSynchronizer
- barging
- uses Unsafe.park() and Unsafe.unpark() for lock * unlock.
Callable
Runnable
Future -
RunnableFuture - FutureTask
ForkJoinTask
BlockingQueue
BlockingDeque
TransferQueue
Exchanger
ConcurrentMap - ConcurrentHashMap
Executor
ExecutorService
CompletionService - ExecutorCompletionService
ScheduledExecutorService - ScheduledThreadPoolExecutor
CyclicBarrier - Reusable, Can take Runnable, - await()
CountDownLatch - One time only - await() & countDown()
Semaphore( int permits) - acquire(), release(), tryAcquire()
http://stackoverflow.com/questions/19660737/aba-in-lock-free-algorithms
CopyOnWriteArrayList
{
final transient ReentrantLock lock = new ReentrantLock();
private transient volatile Object[] array;
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long lockOffset;
}
public boolean add( E )
- Obtain the reentrant lock on the Lock instance associated with the ArrayList instance.
- Create a new array using Arrays.copyOf ( array, size+1)
- set arrayInstance to be the newArray (old one is effectively discarded & should be garbage collected)
- return true & unlock() the lock.
public
No comments:
Post a Comment