Skip to content

Instantly share code, notes, and snippets.

@mageddo
Created March 12, 2023 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageddo/8ab414d0e838b76b6a5ae80dec697de7 to your computer and use it in GitHub Desktop.
Save mageddo/8ab414d0e838b76b6a5ae80dec697de7 to your computer and use it in GitHub Desktop.
Agent.java
import java.util.concurrent.CountDownLatch;
import sun.misc.Signal;
public class Agent {
private final CountDownLatch shutdownLatch;
public Agent() {
this.shutdownLatch = new CountDownLatch(1);
}
public void run() throws InterruptedException {
// Register a signal handler for Ctrl-C that runs the shutdown hooks
Signal.handle(new Signal("INT"), sig -> System.exit(0));
System.out.println("Running");
// Add a shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shutdown();
}));
try {
shutdownLatch.await();
System.out.println("Latch released");
} catch (InterruptedException e) {
System.out.println("InterruptedException");
}
}
public synchronized void shutdown() {
System.out.println("Shutdown called");
shutdownLatch.countDown();
}
public static void main(String[] args) throws Exception {
new Agent().run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment