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
Joined: Sep 23, 2000
Posts: 2912
posted
0
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
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
Joined: May 10, 2001
Posts: 3244
posted
0
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
Dave
Amol Keskar
Greenhorn
Joined: May 18, 2001
Posts: 23
posted
0
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
Joined: May 05, 2000
Posts: 13974
posted
0
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.
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
Joined: Mar 15, 2001
Posts: 23
posted
0
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
Joined: May 10, 2001
Posts: 3244
posted
0
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
Joined: Jul 20, 2001
Posts: 464
posted
0
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).]
Reda
Greenhorn
Joined: Jun 03, 2001
Posts: 1
posted
0
it would depend on how you override the start method. But, you're not supposed to override it. You're supposed to override the run method: public void run() The MyClass (subclass) is a thread by itself in nature. Because it inherits all the Thread related behaviour. if you override the start() method than start() will behave like any other method , then no Thread-like behavior will occur.
same things for the run method, Thread's run() is defined but does nothing. So if you don't override the run() method in a subclass of thread, no errors will occur but the thread will do nothing.
any way. If you do override start() method then you need to include a call to super.start() ... see ligne #5 try to run this code several time and see what's happen
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
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