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

        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();
        }

猜你喜欢

转载自www.cnblogs.com/yejiang/p/12564460.html