Hi,
I was doing the Certification4Career
test 1 and came across the following question:
public class MyThread implements Runnable{
public void run(){
System.out.println("Running MyThread");
}
}//end of MyThread
public class YourThread extends Thread{
public YourThread(Runnable r){
super(r);
}
public void run(){
System.out.println("Running YourThread");
}
}//end of YourThread
public class Driver{
public static void main(
String args []){
MyThread t1= new MyThread();
YourThread t2 = new YourThread(t1);
t2.start();
}
}//end of class
If you try to run Driver class what will be result?
A.It will output "Running MyThread."
B.It will output "Running YourThread."
C.It will output both "Running MyThread," and "Running YourThread."
D.It will not run.
Now I obviously thought A would be the right answer, but the answer is B and I verified this under jdk1.3.1.
Here's the weird thing though. The javadoc states for the constructors of
Thread:
public Thread()
Allocates a new Thread object. This constructor has the same effect as Thread(null, null, gname), where gname is a newly generated name.
public Thread(ThreadGroup�group,Runnable�target)
Allocates a new Thread object. This constructor has the same effect as Thread(group, target, gname), where gname is a newly generated name.
public Thread(ThreadGroup�group,Runnable�target,
String�name)
Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.
Now the interesting part:
If the target argument is not null, the run method of the target is called when this thread is started. If the target argument is null, this thread's run method is called when this thread is started.
Note the order in the documentation. Since the target argument is not null, should it not ignore the "run" method in the class itself? I commented out the run method in the current class and it worked okay. Surely this sounds like either bad doco or a bug in the JVM?
Can anyone see something I'm missing here??
thanks!
Jeremy (5 days to go - the dicey ones are coming out!)