LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Predict the Output · Question 19

Creating virtual threads Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 19 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
    public static void main(String[] args) throws Exception {
        Thread v = Thread.ofVirtual().name("job-1").unstarted(() -> { });
        Thread p = Thread.ofPlatform().name("job-2").unstarted(() -> { });
        Future<Boolean> f;
        try (ExecutorService exec = Executors.newVirtualThreadPerTaskExecutor()) {
            f = exec.submit(() -> Thread.currentThread().isVirtual());
        }
        System.out.println(v.isVirtual() + " " + p.isVirtual() + " " + v.isDaemon() + " " + f.get() + " " + v.getState());
    }
}
  • A true false true true NEW Correct
  • B true false false true NEW
  • C true true true true NEW
  • D true false true false RUNNABLE
Correct Answer: Option A

Virtual threads are always daemon threads. Tasks run by the virtual-thread-per-task executor run on virtual threads; unstarted threads are NEW.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links