• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

atomic operation and Priority

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers ,


i have following queries :

what are atomic operations apart from assignment?

do process have priority in general or is it OS dependent ? And if it is OS dependent then which OS support it ?

 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen yadav wrote:
do process have priority in general or is it OS dependent ? And if it is OS dependent then which OS support it ?


All OSes have some sort of thread priority, and behavior with such a priority. Java will try to map to the OS priority as close as possible; and the scheduling behavior will be OS dependent.

The JLS doesn't specify how the mapping is to be done, nor specify how scheduling works, so that means that pretty much it is easy to be compliant in this regard ...

Henry
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what makes safe to set a priority of a thread as Thread.MAX_PRIORITY but not as any integer value say 10 ?

 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen yadav wrote:
what makes safe to set a priority of a thread as Thread.MAX_PRIORITY but not as any integer value say 10 ?



Not sure what you are asking. What is "safe" in reference to here? Thread safety? Allowable by the OS? Allowable by the JVM? etc.

Henry
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi Henry ,

i have read somewhere that when using setPriority(int ) one should use Thrread.MAX_PRIORITY instead of an integer like 3.

is that true ?
 
Tim Moores
Bartender
Pie
Posts: 2491
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In all likelihood you shouldn't call setPriority at all - it's unlikely to work as you expect it to work, and what's more, that's an area where different JVMs will behave differently, making the code somewhat brittle. (Although, if Henry says otherwise, I would refer to him - he has written the book on this, after all :-)
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen yadav wrote:
i have read somewhere that when using setPriority(int ) one should use Thrread.MAX_PRIORITY instead of an integer like 3.

is that true ?



I guess it would be good not to hard wire values... but I don't see anything inherently unsafe about it.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:In all likelihood you shouldn't call setPriority at all - it's unlikely to work as you expect it to work, and what's more, that's an area where different JVMs will behave differently, making the code somewhat brittle. (Although, if Henry says otherwise, I would refer to him - he has written the book on this, after all


Well, calling setPriority() does set the thread priority. It maps to the underlying threading system's concept of priority. However, two issues...

Some OSes, don't have enough priority levels. When this happens some adjacent java thread priority will map the the same priority level underneath. So, with those OSes, if you try to micro manage it (use all Java thread priority values), you could be doing nothing with certain values.

You are still subjected to the underlying OS scheduler. And with certain OSes, the priority is only a single input into whether a thread should run -- depends on other criteria besides the basic priority level. Some OSes support some sort of mechanism to prevent thread starvation (of low priority threads). And most OSes support priority inheritance, which can also affect the actual priority.

Henry
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi henry ,

Henry Wong wrote: And most OSes support priority inheritance, which can also affect the actual priority.

Henry



will you kindly briefly describe what is priority inheritance ?



 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen yadav wrote:will you kindly briefly describe what is priority inheritance ?



Priority inheritace is when a thread gets a temporary bump in priority, due to what threads are waiting on locks that it owns. With priority inheritence, if a low priority thread owns a lock, that a high priority thread later wants -- it will be bumped to the higher priority. The reason is... the higher priority thread is blocked waiting for this lock, so the low priority thread needs to run at a higher priority in order to finish up and free the lock for the high priority thread. The low priority thread is "inheriting" the high priority of the high priority thread.

Henry
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nice explanation , thanks Henry.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic