Hi,
Theres a question by danchisholm;
class A extends
Thread {
private Object obj;
public A(Object obj) {this.obj = obj;}
public void run() {
try {
synchronized (obj) {obj.wait();}
} catch (InterruptedException ie) {}
System.out.print(Thread.currentThread().getName());
}}
class B {
private void m1() {
for (int i = 0; i < 10; i++) {
A t1 = new A(this);
t1.setName(String.valueOf(i)); t1.setDaemon(true); t1.start();
}
synchronized (this) {notifyAll();}
}
public static void main(
String[] args) {new B().m1();}
}
What are the possible results of attempting to compile and run the program?
a. All of the numbers 0 through 9 must always be printed
b. Some or all of the numbers 0 through 9 could be printed
c. Nothing is printed
d. Run-time error
The answer is b&c.
There are no chances that any number will be printed because daemon thread will die as soon as the main thread finishes.
Is it correct to give a. as an option too?
Thanks in advance.