Forums Register Login

why output is so different

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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.
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 972 times.
Similar Threads
Captcha problem
Understanding the Thread reference that is returned by call to Thread.currentThread();
multithreading not working correctly
why output is so different
Threads execution is disorder
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:25:28.