File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Question on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Question on Threads" Watch "Another Question on Threads" New topic
Author

Another Question on Threads

Amol Keskar
Greenhorn

Joined: May 18, 2001
Posts: 23
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: 3254
    
    2
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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
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
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
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
wei luo
Greenhorn

Joined: Mar 15, 2001
Posts: 23
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
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
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
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Another Question on Threads
 
Similar Threads
Class level lock versus Object level lock
Question ID :988384705515 JQ+
synchronization
preculiar synchronized behaviour
Regarding Threads!!