目 录CONTENT

文章目录

14_JUC_线程调度

已删除用户
2019-08-27 / 0 评论 / 0 点赞 / 29043 阅读 / 0 字

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
广告 广告

评论区