Call method at the end of program execution
I am creating a client library for an API endpoint using Unirest
to simulate requests GET
and POST
. After the program finishes, you need to call the following code to terminate the current thread.
Unirest.shutdown(); // must be called in order to clear the high CPU consuming thread
Is there any possible way to implicitly call this in my client library at the end of program execution?
+3
source to share
1 answer
Yes - your best option is most likely Shutdown Hook . It will be called / executed when the JVM terminates. As an example:
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.out.println("JVM shutting down, closing Unirest");
Unirest.shutdown();
}
}));
Ideally, you should call the method as soon as possible addShutdownHook()
after starting the Unirest service.
+1
source to share