Oracle
1Z0-851 · Question #218
Given: public class Threads4 { public static void main (String[] args) { new Threads4().go(); } public void go() { Runnable r = new Runnable() { public void run() { System.out.print("foo"); } }…
The correct answer is B. An exception is thrown at runtime. Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at Threads4.go(Threads4.java:14) at Threads4.main(Threads4.java:3)
Concurrency
Question
Given: public class Threads4 { public static void main (String[] args) { new Threads4().go(); } public void go() { Runnable r = new Runnable() { public void run() { System.out.print("foo"); } }; Thread t = new Thread(r); t.start(); t.start(); } } What is the result?
Options
- ACompilation fails.
- BAn exception is thrown at runtime.
- CThe code executes normally and prints "foo".
- DThe code executes normally, but nothing is printed.
How the community answered
(63 responses)- A13% (8)
- B75% (47)
- C10% (6)
- D3% (2)
Explanation
Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Unknown Source) at Threads4.go(Threads4.java:14) at Threads4.main(Threads4.java:3)
Topics
#Thread lifecycle#IllegalThreadStateException#Runnable#thread start
Community Discussion
No community discussion yet for this question.