aspose file tools*
The moose likes Threads and Synchronization and the fly likes atomic operation and Priority Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "atomic operation and Priority" Watch "atomic operation and Priority" New topic
Author

atomic operation and Priority

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Oct 23, 2011
Posts: 384


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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Oct 23, 2011
Posts: 384

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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Oct 23, 2011
Posts: 384


nice explanation , thanks Henry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: atomic operation and Priority