美文网首页
JUC(12) - 线程调度

JUC(12) - 线程调度

作者: 21号新秀_邓肯 | 来源:发表于2020-05-18 18:39 被阅读0次

    12. 线程调度

    ScheduledExecutorService: 一个 ExecutorService,可安排在给定的延迟后运行或定期执行的命令。

        public static void main(String[] args) throws ExecutionException, InterruptedException {
            ScheduledExecutorService pool = new ScheduledThreadPoolExecutor(5);
    
            for (int i = 0; i < 5; i++) {
                Future<Integer> result = pool.schedule(new Callable<Integer>(){
    
                    @Override
                    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());
            }
    
            pool.shutdown();
        }
    

    相关文章

      网友评论

          本文标题:JUC(12) - 线程调度

          本文链接:https://www.haomeiwen.com/subject/gnucohtx.html