Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

threads in java

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23906
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 22392
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the reply
 
This one time, at bandcamp, I had relations with a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic