Given:
public class DoubleThread {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
System.out.print(“Greeting”);
}
};
Thread t2 = new Thread(t1); // Line 9
t2.run();
}
}
Which two are true?
A.
A runtime exception is thrown on line 9.
B.
No output is produced.
C.
Greeting is printed once.
D.
Greeting is printed twice.
E.
No new threads of execution are started within the main method.
F.
One new thread of execution is started within the main method.
G.
Two new threads of execution are started within the main method.
Explanation:
Thread t2 is executed. Execution of T2 starts executionen of t1. Greeting is printed during the execution of t1.
A F
public class Q2DoubleThread {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
System.out.print(“Greeting”);
}
};
Thread t2 = new Thread(t1); // Line 9
t2.run();
System.out.println(“\n” + Thread.currentThread().getName());
}
}
output:
Greeting
main
C E
C and E, compiled and runed
C, E
Thread itself implements Runnable so can pass a Thread object to the Thread Constructor.
CE
Eclipse output:
Greeting