aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in thread run's method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in thread run Watch "doubt in thread run New topic
Author

doubt in thread run's method

Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Hi friends,
Please see the code given below, itsfrom Marcus Test one.
public class Testing extends Thread{
private String sThreadName;
public static void main(String argv[]){
Testing h = new Testing();
h.go();
}
Testing(){}
Testing(String s){
sThreadName = s;
}
public String getThreadName(){
return sThreadName;
}
public void go(){
Testing first = new Testing("first");
first.start();
Testing second = new Testing("second");
second.start();
}
public void start(){
for(int i = 0; i < 8; i ++){
System.out.println(getThreadName() +i);
try{
Thread.sleep(100);
} catch(InterruptedException e){System.out.println(e.getMessage());}
}
}
}
1) Compile time error
2) Output of first0, second0, first0, second1
3) Output of first0, first1, second0, second1
4) Runtime error
I understand that no run method makes this code to print ans. 3 but my doubt is once we say
first.start();
this means thread should look for run method which should override run's method in Thread class, so in the absense of run method, shouldn't it give compiler error or run time error rather than print output of for loop once for String "first" and then for "second".
Please clarify.
Jyotsna

Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Jyotsna
The Thread class implements Runnable so it has to provide an implementation for the run() method in Runnable. All it does is create an empty run() method that the subclass inherits. So it does nothing then returns so there would be no errors. If you implemented Runnable, on the other hand, then you'd have to supply a run() or you'd get a compiler error for not providing an implementation for it.
Dave


Dave
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Thanks Dave for explanation. Its clear now.
Jyotsna
April.Johnson
Ranch Hand

Joined: May 02, 2001
Posts: 48
Dave is correct in his post that the default run() method in the Thread class does nothing and that if you want a class that extends Thread to run, you'll have to override the run() method.
However, looking at the code posted, I notice that Thread's start() method is overridden in Testing. In that case, the thread will never really run or start. Overriding start() the way it's written basically means that the threads first and second never run. The method start() is essentially just another method. If you still want threads to run, you'll have to explicitely call super.start() in Testing.start() to experience the joins of live threads.
Try this:
public class Testing extends Thread{
private String sThreadName;
public static void main(String argv[]){
Testing h = new Testing();
h.go();
}
Testing(){}
Testing(String s){
sThreadName = s;
}
public String getThreadName(){
return sThreadName;
}
public void go(){
Testing first = new Testing("first");
first.start();
Testing second = new Testing("second");
second.start();
}
public void start(){
super.start(); // calling Thread.start() to get Thread functionality
for(int i = 0; i < 8; i ++){
System.out.println(getThreadName() +i);
try{
Thread.sleep(100);
} catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
}
// put this in just to show when the thread is running and
// when it's not. Removing this method (while keeping the
// call to super.start()) will do what Dave was talking about.
public void run(){
System.out.println("in run method of " + getThreadName());
}
}
Did I miss something?
April
[This message has been edited by April.Johnson (edited June 27, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in thread run's method
 
Similar Threads
doubt on start and run methods
Question on Thread
Marcus Green mock exam 1, Q.31.
Doubt in Thread run method.
synchronize