3

我正在为 API 端点创建一个客户端库,用于Unirest模拟GETPOST请求。程序完成后,必须调用以下代码才能终止当前线程。

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

在程序执行结束时,是否有任何可能的方法在我的客户端库中隐式调用它?

4

1 回答 1

1

是的 - 您最好的选择可能是Shutdown Hook。当 JVM 终止时,它将被调用/执行。举个例子:

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

理想情况下,您应该在启动 Unirest 服务addShutdownHook()尽快调用该方法。

于 2015-07-29T16:59:23.837 回答