Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Question on Threads

 
Amol Keskar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In the following code,
class MyClass extends Thread
{
public void start()
{
System.out.print("In start()");
run();
}
public void run()
{
System.out.print("In run()");
}
public static void main(String[] args)
{
MyClass m1 = new MyClass();
MyClass m2 = new MyClass();
m1.start();
m2.start();
System.out.print("End of main()");
}
}

Since we have overridden start() method and we are explicitly calling run() method, Are there still 3 threads ( 2 threads started in main and one main thread) or is there just one main thread?
And so can the output be guaranteed to be in this order
In start()In run()In start()In run()End of main() ?
or we cannot guarantee anything about the output??
Thanks,
Amol
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think?
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amol,
My understanding is..
start(), registers your thread to the thread schedular.
start() does not make your thread to run immediately. It only make it eligible to run or "ready-to-run". Schedular at some of point time moves the thread to running state. In this state your run()gets executed.
So the sequence of your output is correct.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amol
Because you overrode the start method and called run directly you'll end up with one thread. Each time you call start and then run they'll execute in order like any normal method call.
The start method in Thread does some behind the scenes work to set up the Tread to run in its own process and not part of the main Thread. The same thing happens if you try to call run directly instead of letting start do it.
To test it, put a call to sleep in the run method and watch your output. It will still run in order and you'll see it pause each time after it prints 'in run'. Then to see it the other way, comment out your start method and then run it again. You'll see main end before either of the two 'in run' messages are printed.
Dave
 
Amol Keskar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Dave,
Even I think there is only thread in the program and that is the main thread.
I included sleep() in the run() method to see if one thread goes to sleep and other thread is executed. But It still gave me the same output though with some time lag due to sleep.
Hence even I think there is only one thread.
Am I right in my reasoning?
Thanks
Amol
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the start() method of Thread:
public synchronized native void start();
So by overriding start() you are failing to run the call to the native code that actually creates the thread.
 
wei luo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Dave:
I tested the code as you said. But I don't understand why "end of main" comes before the two "in run". I know it must be a simple question, but I haven't got it. Thanks.
 
wei luo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I tested it. If we use "threadname1.start(); threadname2.start(); System.out.println("...")", the order of output is not guaranteed. but the order of outputs of the two start() are relatively fixed. Is that true?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the original sample code:

the output is:
In start()
In run()
In start()
In run()
End of main()
Because there are no new threads being created they all run in the main thread and the method calls are handled in order that they are called. Even though they sleep for 2 seconds in their run methods. This is because the start method was overridden and the threads were never set up to run as seperate processes.
Now, in this code:

The output is:
End of main()
In run()
In run()
Because we are calling the real start method which sets up the threads to run as seperate processes. In this particular case the main method got the cpu and finished executing before the two newly created threads. If we changed the main method to pause then one of the other threads may have got the cpu and finished first. There is no guarantee of the order.
Wei, there is never a guarantee of the order of the output, unless you do something to engineer it like joining two threads.

hope that clears it up
Dave
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Amol's example, if i comment the run() method,from the start()
I am unable to get the In run() output
I do however able to get the In run() output, if i dont override
the start() method... (ie comment out start()
altogether)
So my question is ... If i do override start() method and not call run() from start(), how do i get the In run() output?
Any thoughts pleasee?


[This message has been edited by Ragu Sivaraman (edited July 25, 2001).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reda,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic