aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread basic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread basic" Watch "Thread basic" New topic
Author

Thread basic

kiran kumar reddy
Ranch Hand

Joined: Jun 28, 2012
Posts: 94

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
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.


Ping & DNS - my free Android networking tools app
kiran kumar reddy
Ranch Hand

Joined: Jun 28, 2012
Posts: 94

@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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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!


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
kiran kumar reddy
Ranch Hand

Joined: Jun 28, 2012
Posts: 94

thanks all for your explanation.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

kumar reddyss wrote:thanks all for your explanation.

You are welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread basic