aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread doubt?" Watch "Thread doubt?" New topic
Author

Thread doubt?

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
What will happen when you attempt to compile and run the following code?

public class Runt extends Thread{
public static void main(String argv[]){
Runt r = new Runt();
r.run();
}

public void run(){
for(int i=0;i<100;i++)
System.out.println(i);
}
}
1) Compilation and output of count from 0 to 99
2) Compilation and no output
3) Compile time error: class Runt is an abstract class. It can't be instantiated.
4) Compile time error, method start has not been defined

The actually answer is 1).
Why the answer cannot be 2)because the start()method is not mentioned here instead r.run is defined.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Did you run the code? All you have here is a simple call to the run method in the context of the main thread.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,

public class Runt extends Thread{
public static void main(String argv[]){
Runt r = new Runt();
r.run();
}


In the above code you are not starting any code. instead you are just calling an instance method run().

and since you call run() the method would execute printing the values of i from 0 to 99.

keep it in mind that no thread has been scheduled to run. infact you have just called a normal method called run().

Sandy


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
Dear Karu,
See public void run() is a method
when you call it with start() method of thread it registers itself it with the Thread Scheduler and then its when it becomes a new thread,

While you can also call the method by run(); it will work as you call a normal method.

Thanks,
With Regds,
Anand
[ September 18, 2005: Message edited by: anand phulwani ]

Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
yes i have runned the code.

it prints all the value from 0 to 99.

Even if change the r.run() to r.start() it prints the same value.

Can you tell me the exact difference of these two?
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
Dear karu,

The simple difference is when you do start() it becomes a thread,during the progam execution it might run it might not(but it will surely run before program terminates) i.e you can determine the output,while when we call run() method it will print the values 0 to 99 and this is a determined output.

Thanks,
With Regds,
Anand
[ September 18, 2005: Message edited by: anand phulwani ]
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
thanks a lot .
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by anand phulwani:
Dear karu,

The simple difference is when you do start() it becomes a thread,it might run it might not i.e you can determine the output,while when we call run() method it will print the values 0 to 99 and this is a determined output.

Thanks,
With Regds,
Anand


Yes, calling start() causes a new thread to be created that excutes the same run() method in in its own thread context. But the thread will definitely (not it might run it might not) run.
[ September 18, 2005: Message edited by: Barry Gaunt ]
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
Dear Mr Barry,

What i think is before the program ends,the thread will be executed
but while the program is running, you cant guarantee whether it will run or not.

Thanks,
With Regds
Anand
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread doubt?