Coordinating Threads
without Monitors/Intrinsic Locks |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
Lock
Interface:
lock()
- Acquire the lock when it is/becomes free
lockInterruptibly()
- Acquire the lock unless the
current Thread
is interruptedtryLock()
- Acquire the lock only if
it is free (perhaps within a given amount of time)unlock()
- Free the lockReentrantLock
Class:
Thread
that is waiting
to acquire a ReentrantLock
tryLock()
)
ReentrantLock
IdiomReentrantLock
Objects:
ReentrantLock
Objects:
ReentrantReadWriteLock
Objects:
int
named
count
await()
- causes the calling current thread to
enter the wait state until the count
reaches
0 (or it is interrupted, or a given amount of time has
elapsed)countDown()
- decreases the count
(releasing all waiting threads when the count reaches 0)
countDown()
can be called from any thread
(all that matters is the number of times it is called, not the
number of different threads that call it)join()
methods in the Thread
classRunnable
Used in the Examplejoin()
waits for the thread to terminate; barriers
can be used at any pointint
named
parties
and an optional Runnable
named barrierAction
(that will be executed in
on of the threads)await()
- causes the calling current thread to
enter the wait state until the count
reaches
0 (or it is interrupted, or a given amount of time has
elapsed)getNumberWaiting()
- returns the number of
parties currently waiting at the barrier point
isBroken()
- returns true
if
a call to await()
times-out or is interrupted
reset()
- resets the barrier to its initial state
Runnable
Objectsacquire()
methods are given permits on a
first-in-first-out basisLock
, Semaphore
, CountDownLatch
:
CyclicBarrier
, Phaser
: