Class Locks.TickTockLock

java.lang.Object
com.seeq.utilities.Locks.TickTockLock
All Implemented Interfaces:
Closeable, AutoCloseable
Enclosing class:
Locks

public static class Locks.TickTockLock extends Object implements AutoCloseable, Closeable
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 Details

    • createAndTick

      public static Locks.TickTockLock 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 interface AutoCloseable
      Specified by:
      close in interface Closeable