简体   繁体   中英

Calling Method at the End of Program Execution

I am creating a client library for an API endpoint using Unirest to simulate GET and POST requests. Once the program finishes, the following code must be called in order to terminate the current thread.

Unirest.shutdown(); // must be called in order to clear the high CPU consuming thread 

Is there any possible way implicitly call this in my client library at the end of the program's execution?

Yes - your best option is likely a Shutdown Hook . It will be called/executed when the JVM is terminating. As an example:

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    public void run() {
        System.out.println("JVM shutting down, closing Unirest");
        Unirest.shutdown();
    }
}));

You should ideally call the addShutdownHook() method as soon as possible, after you have started the Unirest service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM