Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread basic

 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i am new to thread concept...here is the program which is basic program......


Here in the above program...execution starts from main method(main thread)...it creates object t1 of type 'th' and it goes to t1.start()...calling start method on t1 object..it runs 'run' method....and if it so then it should print 0,1,2,3,4 and then goes to main method and prints 'main method' according to above explanation the output is
0,1,2,3,4
main method
..............but actually it is printing
main method
0,1,2,3,4
...what is the process goin??? help me to understand
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have two threads -like the main thread here and the newly started one- there is no guarantee which one will be scheduled first. This is not sequential code you have here, it is concurrent code - just because something is started sooner, does not mean it will run first.
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ulf Dittmer...okay then what exactly is the procedure.....after reaching start method it will goes to run method in child thread and the same time will it execute print statement in main method ?? some what confusing...actually what is happening ??
 
Henry Wong
author
Marshal
Pie
Posts: 21382
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kumar reddyss wrote:@Ulf Dittmer...okay then what exactly is the procedure.....after reaching start method it will goes to run method in child thread and the same time will it execute print statement in main method ?? some what confusing...actually what is happening ??


Basically. after the main thread calls the start() method, there are now two threads. The original main thread which will return from the start() method, and do other stuff. And a newly created thread which will initialize, and then eventually, call the run() method..... as for the exact order, that is completely dependant on the OS scheduller (assuming modern JVM).

Henry
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kumar reddyss wrote:@Ulf Dittmer...okay then what exactly is the procedure.....after reaching start method it will goes to run method in child thread and the same time will it execute print statement in main method ?? some what confusing...actually what is happening ??

As your doubts have already been answered by Ulf Dittmer and Henry Wong, I would just like to mention that unlike process, there is no parent-child relationship in threads. e.g. if T1 creates a thread T2, then T2 is not a child of T1. T2 will continue the execution even after T1 is finished (or crashed).

I hope this helps.

And Welcome To CodeRanch!
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all for your explanation.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kumar reddyss wrote:thanks all for your explanation.

You are welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic