Class ShutdownSignalBarrier

java.lang.Object
org.agrona.concurrent.ShutdownSignalBarrier
All Implemented Interfaces:
AutoCloseable

public final class ShutdownSignalBarrier extends Object implements AutoCloseable
One time witness for blocking one or more threads until: Useful for shutting down a service.

Note: ShutdownSignalBarrier must be closed last to ensure complete service(s) termination and to allow JVM to exit. Not calling close() method might result in JVM hanging indefinitely.

Here is an example on how to use this API to await service termination.

 
 class UsageSample
 {
   public static void main(final String[] args)
   {
     try (ShutdownSignalBarrier barrier = new ShutdownSignalBarrier();
         MyService service = new MyService())
     {
          barrier.await();
     }
   }
 }

 class MyService implements AutoCloseable
 {
     ...
 }
 

It is also possible to use barrier to implement a flag that is checked in the main thread.

 
 class FlagSample
 {
   public static void main(final String[] args)
   {
     final AtomicBoolean running = new AtomicBoolean(true);
     try (ShutdownSignalBarrier barrier = new ShutdownSignalBarrier(() -> running.set(false))
     {
          while (running.get())
          {
              ...
          }
     }
   }
 }
 
See Also:
  • Constructor Details

    • ShutdownSignalBarrier

      public ShutdownSignalBarrier()
      Construct and register the witness ready for use.
    • ShutdownSignalBarrier

      public ShutdownSignalBarrier(ShutdownSignalBarrier.SignalHandler signalHandler)
      Construct and register the witness ready for use with a signal handler.
      Parameters:
      signalHandler - to notify.
      Throws:
      NullPointerException - if null == signalHandler.
  • Method Details

    • signal

      public void signal()
      Programmatically signal awaiting thread on the latch associated with this witness.
    • signalAll

      public void signalAll()
      Programmatically signal all awaiting threads.
    • remove

      public void remove()
      Remove the witness from the shutdown signals.
    • await

      public void await()
      Await the reception of the shutdown signal.
    • close

      public void close()
      Close this ShutdownSignalBarrier to allow JVM termination.
      Specified by:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object