Package org.agrona.concurrent
Class ShutdownSignalBarrier
java.lang.Object
org.agrona.concurrent.ShutdownSignalBarrier
- All Implemented Interfaces:
AutoCloseable
One time witness for blocking one or more threads until:
- Thread blocked in
await()is interrupted. - JVM shutdown sequence begins (see
Runtime.addShutdownHook(Thread)). signal()orsignalAll()is invoked programmatically.
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for notification when is signaled. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct and register the witness ready for use.ShutdownSignalBarrier(ShutdownSignalBarrier.SignalHandler signalHandler) Construct and register the witness ready for use with a signal handler. -
Method Summary
Modifier and TypeMethodDescriptionvoidawait()Await the reception of the shutdown signal.voidclose()Close thisShutdownSignalBarrierto allow JVM termination.voidremove()Remove the witness from the shutdown signals.voidsignal()Programmatically signal awaiting thread on the latch associated with this witness.voidProgrammatically signal all awaiting threads.toString()
-
Constructor Details
-
ShutdownSignalBarrier
public ShutdownSignalBarrier()Construct and register the witness ready for use. -
ShutdownSignalBarrier
Construct and register the witness ready for use with a signal handler.- Parameters:
signalHandler- to notify.- Throws:
NullPointerException- ifnull == 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 thisShutdownSignalBarrierto allow JVM termination.- Specified by:
closein interfaceAutoCloseable
-
toString
-