• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Thread doubt?

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you run the code? All you have here is a simple call to the run method in the context of the main thread.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot .
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic