Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads.....i thought a compile err would occur

 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i thought a compile err would occur,but it didnt,and output produced is nothing.......im confused,can anybody pls explain this....

public class background extends Thread{
public static void main(String argv[]){
background b = new background();
b.run();
}
public void start(){
for (int i = 0; i <10; i++){
System.out.println("Value of i = " + i);
}
}
}
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No compile error occurs because you override the start(), and then you call the run(). Since you didn't override run(), it does nothing as it would by default.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sean, im still not clear, can u explain in more broad way
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, if you want a thread to have functionality, you extend Class Thread, override the run(), and then create an instance and call start(). By default, the run() of class Thread is empty, so if you don't override it, then you will inherit it, and as I pointed out it is empty. IN your example, the start method is overridden, but the run method isn't. So when b.run() is called it does nothing. Note: You usually wouldn't override the start(), but it is perfectly legal.
In order to cover my tracks on my first statement: Of course you could also implement Runnable whereas you'd be required to override the run().
[This message has been edited by Sean Casey (edited July 03, 2001).]
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks sean i got it
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic