aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why stat's after start() gets printed out 1st ?? 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 "why stat Watch "why stat New topic
Author

why stat's after start() gets printed out 1st ??

srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
can anybody pls explain this..
why does this program print "in main after t start","just to test", 1st
rather than printing "Thread running" 1st. control goes to s.o.p stat's 1st after which the control goes to t.start() , why? why does the jvm leave the t.start() at 1st and execute s.o.p 1st .. i even tried setting thread t priority high and then tested, but still it works the same.
public class scaryThread implements Runnable
{
public void run(){
for(int x=0;x<2;x++)
{
System.out.println("Thread Running");
}
pls_runme();
}

void pls_runme()
{
System.out.println("gooofy stuff..!!");
}

public static void main(String a[])
{
Thread t = new Thread(new scaryThread());
t.start();
System.out.println("in main after t start");
System.out.println("just to test");
}
}


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
good q!
All threads are spawned from the main thread.
so they inherit the main thread's priority.
if you want it to work the way that you described, i.e. run() method gets executed first, try the following code in main method


Amir
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
minor error
Thread t = new Thread(new scaryThread());
instead of Thread t = new Thread(new NewTest());
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
hi amir,thanx for the reply but...
my curiosity is not the answer/result. i thought generally the statements are executed in the order which they are coded. now t.start() is coded prior to println statements, which looks for run() and execute that, and then return to the place after start(), and then execute whatever..after, but, that is not happening.why?
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Originally posted by Amir Ghahrai:
good q!
All threads are spawned from the main thread.
so they inherit the main thread's priority.
if you want it to work the way that you described, i.e. run() method gets executed first, try the following code in main method

Please tell me when wait() is called on object t it releases the lock right? Then how can same object invoke notify().
I have tested and this code work fine. Is it becoz there are no other threads waiting for the lock??
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
Actually, here the call to notify() is redundant, as there is only one thread. I'm just used to using wait() and notify() together.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi srinivas,
The start() method doesn't look for run() method.
Its job is to prepare a thread for running, but
not to field the thread immediately. It is the
OS which decide when to field a thread.
After start() have done its job, it returns to
main() and main() keeps doing what is waiting to
be executed until the OS stops it and transfer the execution right to the newly created thread.
The OS determine everything.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Just to clarify:

There is no guarantee which thread will get executed after these statements. The output of your program is entirely platform-dependent, so there's no guarantee what your output will be. Btw, thread priorities are also platform-dependent, so you shouldn't use this to schedule your thread's execution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why stat's after start() gets printed out 1st ??