Created
March 12, 2023 16:29
-
-
Save mageddo/8ab414d0e838b76b6a5ae80dec697de7 to your computer and use it in GitHub Desktop.
Agent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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