Tuesday, August 17, 2010

Multithreading: Needle.java

Courtesy of Sun Web Learning Center

 4. class Needle implements Runnable {
 5.   public static void main(String [] args) {
 6.     // insert code here
 7.     t.start();
 8.   }
 9.   public void run() { ; }
10. }
And these four fragments of code:

I.   Thread t = new Needle();
II.  Needle t = new Needle();
III. Thread t = new Thread(new Thread());
IV.  Thread t = new Thread(new Needle());

Which, inserted independently at line 6, will compile?

A Only fragment IV
B Only fragment III
C Only fragments I and II
D Only fragments III and IV
E Only fragments I, III, and IV
F All four fragments will compile

D is correct. A thread is needed for line 7 to compile, and the last two fragments create a thread.

No comments:

Post a Comment