• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Thread Constructor Does Not Act As Per Documentation!

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!)
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ September 22, 2002: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Javadoc for the thread class is correct. If you pass a Runnable target object to the constructor then invoking Thread.run will invoke the run method of the target object. The following is the source code for Thread.run.

In your example, however, the Thread.run method was not invoked. Instead, the run method of your subclass was invoked.
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic