Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

which executes first?  RSS feed

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Following code:





OUTPUT ON COSOLE:
-------------------------------
main
* Inside g() // LINE 3
Leaving g()
Thread-0
* Inside f()
Leaving f()



My Question is:
---------------
Why its first of all going inside the mehtod g() like shown in LINE 3.
Accoring to me it should execute f() mehtod 1st? As i am calling f() mehtod first like shown in LINE 1.

Every time i execute it always goes inside g() mehtod first. Why?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul:
Why its first of all going inside the mehtod g() like shown in LINE 3.
Accoring to me it should execute f() mehtod 1st? As i am calling f() mehtod first like shown in LINE 1.


Line 1 is executed from inside the new thread you are starting.
start() will return when a new thread is started(not necessarily that run method has executed)
Since, after start returns there are two concurrent threads running, it depends on timing as to which method gets executed first(f() or g()).
In some cases, it may so happen that f() gets executed first but it all depends on timing.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean there are 2 threads One is Main and other one is which i am starting explicitly. And g() method will be executed by Main thread?
Isn't it!

So in my case the main thread continues to execute g() method first and then it gives chance to run other thread to execute f().
Is it so!

One more thing i already asked that i executed my programm several times and believe me it first going in g() method always.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Nitesh said, the execution sequence of threads in a JVM process is OS scheduler dependent,assuming we are not dealing with green (user space) threads here.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in my case the main thread continues to execute g() method first and then it gives chance to run other thread to execute f().


No. As I said in my earlier post,there's no guarantee that the main thread would execute to completion before the child thread executes. Also it's not necessary that a thread will execute to completion before another thread even starts to execute.The scheduler can pre-empt a running thread to make the CPU(s) available to another thread. The sequencing of execution of threads is BEYOND the programmer's control. However, there are ways,supported by Java's thread library, to enforce some sequence of execution of threads,in case the application so demands. BUT,in general,the execution sequence determination is the scheduler's responsibility,and a programmer should develop a multi-threaded program keeping this in mind and make no implicit assumption about the possible execution sequence of the threads.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing i already asked that i executed my programm several times and believe me it first going in g() method always.


Well, the behavior you are observing "might" change on a different platform ( a different OS).It may also change on the same platform with an increase in the number of threads spawned in the JVM and the state of allocation of system resources to different threads in different processes.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ajay and and thanks to all. Now picture is clear. I got the concept.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!