目 录CONTENT

文章目录

14_JUC_线程调度

ByteNews
2019-08-27 / 0 评论 / 0 点赞 / 25,445 阅读 / 558 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-01-16,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

14_JUC_线程调度

ScheduledExecutorService

public class TestScheduledThreadPool {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);

        for (int i=0;i<5;i++) {
            Future<Integer> result = pool.schedule(new Callable<Integer>() {
                public Integer call() throws Exception {
                    int num = new Random().nextInt(100);
                    System.out.println(Thread.currentThread().getName() + ":" + num);
                    return num;
                }
            }, 1, TimeUnit.SECONDS);
            System.out.println("result.get() = " + result.get());
        }
        pool.shutdown();

    }

}
0

评论区