File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
There are two threads. Can anyone explain what's happening? The threads are executed in a manner that only after one thread is completely executed the next thread is executed and not interchanging the execution.




Output is:

This is 1.
This is 2.
This is 3.
This is 4.
This is 5.
This is 6.
This is 7.
This is 8.
This is 9.
This is 10.
This is 1.
This is 2.
This is 3.
This is 4.
This is 5.
This is 6.
This is 7.
This is 8.
This is 9.
This is 10.


Regards,<br /> <br />MannY<br />----------------------------------------------------------- <br />"Beam me up Scotty, there's no intelligent life down here" !!! <br />------------------------------------------------------------
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why should the threads interchange their orders of execution?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
hmmm, but I guess when you increase the array elements which I did... One thred should not dominate the cpu time. They should share the cpu as both have equal priority.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Possibly, but why should they?
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
You have a point Barry... This means we can't possibly predict the output it can be anything right? What if such questions come on certifications?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
That option is usually among the choices. Now try to find out what you can do to force the execution to interchange amongst the threads.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
MannY? You sleepin?
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Oh Sorry barry... I got your point...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Um... oh never mind.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Hey Barry please check my private message.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by MannY Gates:
You have a point Barry... This means we can't possibly predict the output it can be anything right? What if such questions come on certifications?


You can indeed not predict the order of execution.
Make the loops longer.
When they get to a few hundred cycles you'll start to see them alternating in a seemingly random manner.

On your machine and JVM the Thread scheduler just didn't swap out the first thread before it completed. On a slower machine it may well do so.


42
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Yes I agree with you Jeroen.
Thanks for pitching in.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads