This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why output is so different  RSS feed

 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
i was running a program that has multiple threads with different priorities 5,6,2

C:\>java ThreadPri
start thread P
start thread Q
start thread R
end of main Thread
Thread p started
Thread[Thread-0,5,main]
from thread P: i =1
from thread P: i =2
from thread P: i =3
from thread P: i =4
Exit from P
Therad Q started
Thread[Thread-1,6,main]
from thread Q: j =1
from thread Q: j =2
from thread Q: j =3
from thread Q: j =4
Exit from Q
Therad R started
Thread[Thread-2,2,main]
from thread R: k =1
from thread R: k =2
from thread R: k =3
from thread R: k =4
Exit from R


I made some changes in program . now priorities are 5,7,2


C:\>java ThreadPri
start thread P
start thread Q
Therad Q started
Thread[Thread-1,7,main]
from thread Q: j =1
from thread Q: j =2
from thread Q: j =3
from thread Q: j =4
Exit from Q
start thread R
end of main Thread
Thread p started
Thread[Thread-0,5,main]
from thread P: i =1
from thread P: i =2
from thread P: i =3
from thread P: i =4
Exit from P
Therad R started
Thread[Thread-2,2,main]
from thread R: k =1
from thread R: k =2
from thread R: k =3
from thread R: k =4
Exit from R


why output is so different ? the pattern of priority is almost same in both cases ,
my opinion : program running correctly in second case

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main thing to remember is that with multiple threads you've got very little control over the scheduling (unless you intervene with synchronized blocks or other mechanisms). The priorities are just a hint to the scheduler, but they may or may not have a great effect. In fact, you can potentially get very different results running the program twice with the same priorities.

The key thing to take away from this is to never make assumptions over how the threads will be handled when left to their own devices. If the order really matters to your application, you need to design that in.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!