Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
Thread t = new Thread(() -> {
String name = Thread.currentThread().getName();
System.out.println(name);
}, "cook");
t.run();
}
}main
- Expecting
cook:run()does not start a thread, so the task runs inmain, andThread.currentThread()returns the main thread. - Expecting nothing because the thread was never started:
run()is an ordinary call, so the task runs at once in the calling thread.
- What would the program print if
t.run()were replaced witht.start()? - What happens if you call
start()on the same thread twice?