线程池,设置线程执行超时异常

        ExecutorService service = Executors.newSingleThreadExecutor();
        Runnable task = () -> {
            System.out.println("start");
            try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
        };
        Future<?> future = service.submit(task);
        try {

            future.get(4, TimeUnit.SECONDS);
            System.out.println("end...");
        } catch (Exception e) {
            e.printStackTrace();
        }
posted @ 2020-03-25 10:38  木叶小寒江  阅读(1688)  评论(0编辑  收藏  举报