nerdexam
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)
  • A
    13% (8)
  • B
    75% (47)
  • C
    10% (6)
  • D
    3% (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.

Full 1Z0-851 Practice