Friday, August 27, 2010

Multithreading: Pins.java

Courtesy of Sun Web Learning Center

Given:

5. public class Pins implements Runnable {
6.    public static void main(String[] args) {
7.       Pins p = new Pins();
8.       Thread t1 = new Thread(p);
9.       Thread t2 = new Thread(p);
10.     t1.start();
11.     t2.start();
12.    t1.start();
13. }
14.  public void run() { System.out.print("x"); }
15. }

What is the result?


A x
B xx
C xxx
D Compilation fails.
E The code runs with no output.
F An exception is thrown at runtime.

F is correct. Multiple threads can be created using the same Runnable object, but a given thread can be started only once.

No comments:

Post a Comment