Package com.seeq.utilities
Class Locks.TickTockLock
java.lang.Object
com.seeq.utilities.Locks.TickTockLock
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Enclosing class:
- Locks
Enables the synchronization of tasks between two threads in a lockstep manner, where only one thread can be
executing tasks at a time. The idea is that a tick must always be followed by a tock, so when a second tick is
encountered, control shifts to the thread blocked on a tock.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Does not block.static Locks.TickTockLock
The thread invoking this will continue to run until it encounters another tick.void
tick()
void
tock()
-
Method Details
-
createAndTick
The thread invoking this will continue to run until it encounters another tick. When that happens, control switches to the thread blocked on the tock. That thread will then continue until it encounters another tock in which case the previous thread that is blocked on a tick will resume.Note: Dustin is the original author of this, and he's not quite happy with it. It's confusing to use. Does anyone know of something better to synchronize two threads in lockstep like this?
- Returns:
- A lock that has been ticked.
-
tick
public void tick() -
tock
public void tock() -
close
public void close()Does not block. Unblocks all invocations of tick() and tock().- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-