Machine-translated by Google. Questions are authored in English.
Top

Discussion

Sample52 is run - what will be the output?

import java.util.concurrent.*;

public class Sample52
{
    public static void main(String[] args) throws Exception
    {
        ExecutorService pool = Executors.newFixedThreadPool(2);
        Future<?> f = pool.submit(() -> System.out.print("ran "));
        System.out.print(f.get() + " ");
        pool.shutdown();
        System.out.print(pool.isShutdown());
    }
}

  • A.ran true
  • B.null ran true
  • C.ran ran true
  • D.ran null true

Answer: D

Submitting a Runnable gives a Future whose get returns null once the task has finished, because a Runnable produces no value. The get also guarantees the task's own output comes first. shutdown returns immediately and isShutdown then reports true.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.