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

threads in java  RSS feed

 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please see the code below.I have a query regarding this at the end of this code.
This is taken from complete reference book.

//create a second thread


The output of this code is:
Child thread:Thread[Demo Thread,5,main]
main thread:5
Child thread:5
Child thread:4
Child thread:3
main thread:4
Child thread:2
main thread:3
Child thread:1
Exiting Child thread
main thread:2
main thread:1
Exiting main thread

This output is not clear to me.As far as i understand when a thread is created it start when its start() method is called because the start method calls the overridden method run().

So when the demo thread is created in the NewThread constructor then the start method calls the run and hence FOR loop should be executed once before going to sleep and after that the for loop for MAIN thread should be executed once.so the output as I understand should be but it is not so

Child thread:Thread[Demo Thread,5,main]
Child thread:5
main thread:5
Child thread:4
Child thread:3
main thread:4
Child thread:2
main thread:3
Child thread:1
Exiting Child thread
main thread:2
main thread:1
Exiting main thread

[edit]Add code tags. CRit]
[ June 04, 2008: Message edited by: Campbell Ritchie ]
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i understand when a thread is created it start when its start() method is called because the start method calls the overridden method run()


The start() methods does *not* call the run() method. The start() method, starts the new thread, which in turn calls the run() method. Once the start() method starts the new thread, they are now in parallel. The run() method could run first, the main thread which created the new thread could run first, and they can context switch at any time.

Henry
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when the demo thread is created in the NewThread constructor then the start method calls the run and hence FOR loop should be executed once before going to sleep and after that the for loop for MAIN thread should be executed once.so the output as I understand should be but it is not so


You can not predict the output of Java Thread program (unless you are using yield and join ), The flow in which java thread executes, totally depends upon the job scheduling algorithm of underlaying OS and little bit on their priorities.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sagar Rohankar:
You can not predict the output of Java Thread program (unless you are using yield and join ), The flow in which java thread executes, totally depends upon the job scheduling algorithm of underlaying OS and little bit on their priorities.

Even with yield you cannot predict anything. join lets you have some control but only that the current thread must wait until another one is finished.

Without using the java.util.concurrent package you still can control thread output by using synchronization using wait, notify and notifyAll, but without any of these mechanisms you're right: the output is totally unpredictable.
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the reply
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!