aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion in Thread 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 "Confusion in Thread" Watch "Confusion in Thread" New topic
Author

Confusion in Thread

ersatish kumar
Greenhorn

Joined: Nov 17, 2011
Posts: 6
Hi,

I have doubt in this program.



How " new NewThread();" is calling " class NewThread() implements Runnable" ???

second doubt is when t.start() is calling then why main thread is printing 1st? According to me Child Thread should print 1st...

Can someone please explains the flow...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

It's easier to look at if your code is formatted usefully. I've done that for you - see UseCodeTags for more info.

Here's what's happening:
- The main method is called
- On line 26 that instantiates a NewThread object
- That calls the NewThread constructor
- On line 6, that creates a new Thread, and passes in the newly created NewThread object into the constructor. This is possible because NewThread implements Runnable, and Thread has a constructor that takes a Runnable object
- On line 8, start() is called on the new Thread, which will cause the run() method of the Runnable to be executed in a new thread of execution.

So you've then got two threads runnings - the main one, and the one you've started. It's completely up to the JVM to determine what order it executes them in, so you can't guarantee which of the two will print first.



ersatish kumar
Greenhorn

Joined: Nov 17, 2011
Posts: 6
1st of all thanks Matthew for clarifying my doubts. I am new to this forum. so i will keep your suggestion in my mind before posting the code
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

nice explain Matthew Brown


with regards,
Shivdhway Pandey
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

Agree, this should be the way to answer, +1 for that.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
Matthew Brown wrote:It's easier to look at if your code is formatted usefully. I've done that for you - see UseCodeTags for more info.

Here's what's happening:
- The main method is called
- On line 26 that instantiates a NewThread object
- That calls the NewThread constructor
- On line 6, that creates a new Thread, and passes in the newly created NewThread object into the constructor. This is possible because NewThread implements Runnable, and Thread has a constructor that takes a Runnable object
- On line 8, start() is called on the new Thread, which will cause the run() method of the Runnable to be executed in a new thread of execution.

So you've then got two threads runnings - the main one, and the one you've started. It's completely up to the JVM to determine what order it executes them in, so you can't guarantee which of the two will print first.





hello Matthew Brown

you have given very BEST explanation about Thread i understood very well in manner

But Sir, about that statement "It's completely up to the JVM to determine what order it executes them in, so you can't guarantee which of the two will print first. " i agree but... i read it some where its all depends on Scheduler who runs the particular Thread so is it "JVM that Scheduler" or "OS's Scheduler"..?

i read it that why this Doubt comes in my mind...!!

Thanking you..!
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

amey dear its true that you are thinking its os scheduler that operates even all process and thread is also process
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

Amey Ambulgekar wrote:[But Sir, about that statement "It's completely up to the JVM to determine what order it executes them in, so you can't guarantee which of the two will print first. " i agree but... i read it some where its all depends on Scheduler who runs the particular Thread so is it "JVM that Scheduler" or "OS's Scheduler"..?


To be honest, I'm not sure. I don't think it's part of the specification, though, which means that JVM writers are free to use their own scheduler or use the OS scheduler as long as the overall behaviour meets the Java specification. Which means that, fortunately, this is one of the many things you don't need to worry about unless you're really interested in the inner workings of the JVM.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

What is important is that you should not rely on the order. mean don't assume that thread will run on same order in which there start() method called. if you need order or sequential execution there are different ways to do it for example by using Thread.join() you can ask one Thread to start processing after another thread.
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

clear your concept then leave the order of printing dear because it will print differently on different os and system
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
Matthew Brown wrote:
Amey Ambulgekar wrote:[But Sir, about that statement "It's completely up to the JVM to determine what order it executes them in, so you can't guarantee which of the two will print first. " i agree but... i read it some where its all depends on Scheduler who runs the particular Thread so is it "JVM that Scheduler" or "OS's Scheduler"..?


To be honest, I'm not sure. I don't think it's part of the specification, though, which means that JVM writers are free to use their own scheduler or use the OS scheduler as long as the overall behaviour meets the Java specification. Which means that, fortunately, this is one of the many things you don't need to worry about unless you're really interested in the inner workings of the JVM.


hi Matthew Brown

thank you for your answer . I got that ..

I am doing preparation of JAVA that's why i asked that Q..

as i am JAVA beginner so.. about book... is "Thinking in JAVA" good..?

Thanking you..

shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

try head first java book for easier clear concepts
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4464
    
    8

Thinking In Java is excellent - at least, the earlier edition I read years ago is (I haven't read the newer ones). Head First Java gets lots of recommendations round these parts as well.
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 67

CLEAR CONCEPT
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Confusion in Thread