This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion in Thread

 
ersatish kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice explain Matthew Brown
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, this should be the way to answer, +1 for that.
 
Amey Ambulgekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amey dear its true that you are thinking its os scheduler that operates even all process and thread is also process
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clear your concept then leave the order of printing dear because it will print differently on different os and system
 
Amey Ambulgekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try head first java book for easier clear concepts
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CLEAR CONCEPT
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic