T
- the type of that lockpublic class Lock<T>
extends java.lang.Thread
implements java.io.Closeable
lock()
that lock and hold it until unlock()
get invoked.Modifier and Type | Field and Description |
---|---|
protected static int |
CLOSE
The lock holder should end it's thread.
|
protected T |
lock
The targeted lock.
|
protected static int |
LOCK
The lock holder should gain it's targeted lock.
|
protected java.lang.Thread |
master
The thread that have created this.
|
protected java.util.concurrent.atomic.AtomicInteger |
state
The reference to communicate with the lock holder thread.
|
protected static int |
UNLOCK
The lock holder should release it's targeted lock.
|
Constructor and Description |
---|
Lock()
Initialise a new lock.
|
Lock(T lock)
Initialize a new lock holder.
|
Modifier and Type | Method and Description |
---|---|
private void |
assertMasterThread()
Assert that the caller thread is the master of this lock.
|
private void |
assertThisThread()
Assert that the caller thread is this lock.
|
void |
close()
Closes this stream and releases any system resources associated
with it.
|
protected void |
close0()
Do code before closing the lock.
|
void |
lock()
Hold the lock.
|
protected void |
lock0()
Sleep with while owning the lock.
|
void |
run()
If this thread was constructed using a separate
Runnable run object, then that
Runnable object's run method is called;
otherwise, this method does nothing and returns. |
void |
unlock()
Release the targeted lock by this.
|
protected void |
unlock0()
Sleep until notified.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
protected static final int CLOSE
protected static final int LOCK
protected static final int UNLOCK
protected final T lock
protected final java.lang.Thread master
protected final java.util.concurrent.atomic.AtomicInteger state
public Lock(T lock)
lock
- the targeted lockjava.lang.NullPointerException
- if the given lock is nullpublic Lock()
public void close()
As noted in AutoCloseable.close()
, cases where the
close may fail require careful attention. It is strongly advised
to relinquish the underlying resources and to internally
mark the Closeable
as closed, prior to throwing
the IOException
.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
IllegalThreadException
- if the caller thread isn't the owner thread of this lockpublic void run()
Runnable
run object, then that
Runnable
object's run
method is called;
otherwise, this method does nothing and returns.
Subclasses of Thread
should override this method.
run
in interface java.lang.Runnable
run
in class java.lang.Thread
IllegalThreadException
- if the caller thread is not thisThread.start()
,
Thread.stop()
,
Thread.Thread(ThreadGroup, Runnable, String)
public void lock()
java.lang.IllegalThreadStateException
- if this lock already closedIllegalThreadException
- if the caller thread isn't the master of thispublic void unlock()
IllegalThreadException
- if the caller thread isn't the master of thisprotected void close0()
IllegalThreadException
- if the caller thread isn't this threadprotected void lock0()
IllegalThreadException
- if the caller thread isn't this threadprotected void unlock0()
IllegalThreadException
- if the caller thread isn't this threadprivate void assertMasterThread()
IllegalThreadException
- if the caller thread isn't the master of thisprivate void assertThisThread()
IllegalThreadException
- if the caller thread isn't this thread