java代码运行时间超时exception方法

    xiaoxiao2021-03-26  29

    final ExecutorService service = Executors.newSingleThreadExecutor();

    Callable<String> theCode = new Callable<String>() {

    public String call() {}

    };

    try {

    final Future<?> future = service.submit (theCode);

    sReturn = (String) future.get(4, TimeUnit.SECONDS);

    }

    catch (InterruptedException | ExecutionException | TimeoutException e) {

    logger.warning("fail." + e.getMessage() + e.toString());

    }

    finally {

    service.shutdown();

    }

    callable或runnable,使代码运行在另一个线程,主线程中计时控制而已,抛出timeout后theCode还可以继续运行直至结束,因为是两个不同的线程。

    暂存未验证
    转载请注明原文地址: https://ju.6miu.com/read-450003.html

    最新回复(0)