• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should the threads interchange their orders of execution?
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly, but why should they?
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MannY? You sleepin?
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Sorry barry... I got your point...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um... oh never mind.
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Barry please check my private message.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I agree with you Jeroen.
Thanks for pitching in.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic