⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timedrun.java

📁 java concurrency in practice 源码. JAVA并发设计
💻 JAVA
字号:
package net.jcip.examples;import java.util.concurrent.*;import static net.jcip.examples.LaunderThrowable.launderThrowable;/** * TimedRun * <p/> * Cancelling a task using Future * * @author Brian Goetz and Tim Peierls */public class TimedRun {    private static final ExecutorService taskExec = Executors.newCachedThreadPool();    public static void timedRun(Runnable r,                                long timeout, TimeUnit unit)            throws InterruptedException {        Future<?> task = taskExec.submit(r);        try {            task.get(timeout, unit);        } catch (TimeoutException e) {            // task will be cancelled below        } catch (ExecutionException e) {            // exception thrown in task; rethrow            throw launderThrowable(e.getCause());        } finally {            // Harmless if task already completed            task.cancel(true); // interrupt if running        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -