This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes why output is so different Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

why output is so different

abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
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
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Thread scheduling is uncontrollable. You can provide hints but it's up to the scheduler to determine the actual scheduling. It might even ignore the priorities. So one reason why the output is different could be because the scheduler decided to schedule the thread in a different way.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please use OneThreadPerQuestion. Since this question is more suitable in Threads and Synchronization I'll close this one.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why output is so different