Win a copy of Design for the Mind this week in the Design forum!
    Bookmark Topic Watch Topic
  • New Topic

why output is so different

 
abhay jain
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please use OneThreadPerQuestion. Since this question is more suitable in Threads and Synchronization I'll close this one.
 
    Bookmark Topic Watch Topic
  • New Topic