• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
}
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
minor error
Thread t = new Thread(new scaryThread());
instead of Thread t = new Thread(new NewTest());
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic