• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Priority

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

could anybody help me to understand more about priorities? I've created the code that follows hoping the Thread("C") with higher priority among the other 2 threads to finish before all others.
but analyzing the output, I can't get it. I read on K&B book that I cannot rely on thread priorities, but I think setting priorities at so much difference will lead me to a more precisely priority code.

The code below, was tested at linux and windows SOs and the output wasn't the expected for me.



And here an output example that I didn't expect:

B 0
B 1
A 0
C 0
B 2
B 3
A 1
A 2
A 3
A 4
A -- finished --
B 4
B -- finished --
C 1
C 2
C 3
C 4
C -- finished --

 
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have three things to see actually, Thread C is started at last, (A and B is in progress); random sleep is *again* random; You cant rely on priorities. Basically these multiplication of probabilities are making things worse to analyze.

I tried same code with 1) no sleep 2) Constant sleep 1000
In first Case: Thread-C finished well in advance (even though it was started last)
In second Case: Thread-C finished before B but could not beat A


I further tried to find reason for second case, the problem was small loop, I increased max(i) to 1000 (and modified constant sleep 1000 to 10) and now C finished before A and B (not well before but yes it finished before)

I tried to re-check the effect by varying sleep constant and range of i; The more I allow threads to work, the chance of getting C first finished gets increased. Eg. I tried Sleep constant 1 and max(i) 10000 (removing unnecessary sysouts for faster processing) and C finished well in advance.


Similar maths can actually help, but in practice result can really vary!
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adolfo,

The results on your system may not be the same as on the fellow sitting next to you and that is what K&B book meant in making the statement quoted by you.
 
AmanZeeK Verma
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

your system may not be the same as on the fellow sitting next to you and that is what K&B book meant in making the statement quoted



This is not only meaning of the statement ...the same code when executed on same machine can also produce different results, if thread priority is the criteria to determine the output.
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AmanZeeK Verma wrote:

your system may not be the same as on the fellow sitting next to you and that is what K&B book meant in making the statement quoted



This is not only meaning of the statement ...the same code when executed on same machine can also produce different results, if thread priority is the criteria to determine the output.



Thanks for adding to that. Even the same computer can give different output at different times.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But what I understand is that java uses Preemptive Scheduling it means if a lower priority thread is having CPU and an another high priority thread is waiting for CPU , java thread scheduler will get the CPU from low priority thread and provide it to high priority thread.

or if multiple threads are waiting for CPU , java thread schedule choose the thread with highest priority.

Also its worth noting that when a Java thread is created, it inherits its priority from the thread that created it.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic