12. Thread scheduling

ScheduledExecutorService: a ExecutorService, can be arranged in a given delay operation or periodic execution of command.

    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(a) throws Exception {
                    int num = new Random().nextInt(100);// Generate a random number
                    System.out.println(Thread.currentThread().getName() + ":" + num);
                    returnnum; }},1, TimeUnit.SECONDS);

            System.out.println(result.get());
        }

        pool.shutdown();
    }
Copy the code