栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用此自定义ExecutorService使关机正常工作?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用此自定义ExecutorService使关机正常工作?

我进行了自定义修改,

TimeoutThreadPoolExecutor
并且工作正常。

public static class TimeoutThreadPoolExecutor extends ThreadPoolExecutor{    private final long timeout;    private final TimeUnit timeoutUnit;    private boolean isShutdown = false;    private final ScheduledExecutorService timeoutExecutor = Executors.newSingleThreadScheduledExecutor();    private final ConcurrentMap<Runnable, ScheduledFuture> runningTasks = new ConcurrentHashMap<Runnable, ScheduledFuture>();    public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, long timeout, TimeUnit timeoutUnit) {        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);        this.timeout = timeout;        this.timeoutUnit = timeoutUnit;    }    public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, long timeout, TimeUnit timeoutUnit) {        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);        this.timeout = timeout;        this.timeoutUnit = timeoutUnit;    }    public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler, long timeout, TimeUnit timeoutUnit) {        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);        this.timeout = timeout;        this.timeoutUnit = timeoutUnit;    }    public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, long timeout, TimeUnit timeoutUnit) {        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);        this.timeout = timeout;        this.timeoutUnit = timeoutUnit;    }    @Override    public void shutdown() {        isShutdown = true;        super.shutdown();    }    @Override    public List<Runnable> shutdownNow() {        timeoutExecutor.shutdownNow();        return super.shutdownNow();    }    @Override    protected void beforeExecute(Thread t, Runnable r) {        if(timeout > 0) { final ScheduledFuture<?> scheduled = timeoutExecutor.schedule(new TimeoutTask(t), timeout, timeoutUnit); runningTasks.put(r, scheduled);        }    }    @Override    protected void afterExecute(Runnable r, Throwable t) {        ScheduledFuture timeoutTask = runningTasks.remove(r);        if(timeoutTask != null) { timeoutTask.cancel(false);        }        if (isShutdown) timeoutExecutor.shutdown();    }    class TimeoutTask implements Runnable {        private final Thread thread;        public TimeoutTask(Thread thread) { this.thread = thread;        }        @Override        public void run() { thread.interrupt(); System.out.println("Cancelled");        }    }}

情况1:无超时

final TimeoutThreadPoolExecutor executorService = new TimeoutThreadPoolExecutor(    100, 100, 0L, TimeUnit.MILLISECONDS, new linkedBlockingQueue<Runnable>(),    6, TimeUnit.SECONDS);executorService.submit(new Callable<Object>(){    @Override    public Object call() throws Exception    {        Thread.sleep(5000);        System.out.println("Done");        return null;    }});executorService.shutdown();executorService.awaitTermination(1, TimeUnit.DAYS);System.out.println("Program done");

它打印:

Task doneProgram done

情况2:超时

final TimeoutThreadPoolExecutor executorService = new TimeoutThreadPoolExecutor(    100, 100, 0L, TimeUnit.MILLISECONDS, new linkedBlockingQueue<Runnable>(),    3, TimeUnit.SECONDS);executorService.submit(new Callable<Object>(){    @Override    public Object call() throws Exception    {        Thread.sleep(5000);        System.out.println("Task done");        return null;    }});executorService.shutdown();executorService.awaitTermination(1, TimeUnit.DAYS);System.out.println("Program done");

它打印:

CancelledProgram done


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/514513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号