aspose file tools*
The moose likes Threads and Synchronization and the fly likes Problems with thread 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 "Problems with thread priority" Watch "Problems with thread priority" New topic
Author

Problems with thread priority

sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43

In this code I expected the output would be
CRM Batch 1
MDM Batch 1
PEGA Batch 1
and so on...but the output is coming in random order.why is that?why does the setpriority does not have any effect?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You're best off not fiddling with (or relying on) the priorities of threads. It's just something that in practice doesn't work like the theory suggests it should.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

In addition to the fact that the schedule is free to use the priority as it sees fit (or ignore it altogether), your threads do things that make priority much less relevant:

1) sleep()

2) I/O

3) Very little CPU time needed

Even if a particular scheduler rigorously respects priority, I would only expect that to matter for threads that are a) ready for CPU time at the same time and b) competing for the same CPU or core. If a thread is sleeping or waiting on I/O, there's no reason for the scheduler not to give time to another thread, even one of lower priority. That would just be wasteful. And since neither sleep nor I/O are predictable as to when they'll be done, there's no reason to assume that all your threads will be ready for CPU at the same time. In addition to that, there's probably some minimum time slice that the scheduler will give a thread before bumping it out in favor of a higher priority one, and your threads need so little CPU time at one shot before they sleep or do I/O that it's not likely the scheduler even gets a chance to bump one out.

If you do a more CPU-intensive task, longer running without so may interruptions in your code, you might see some effect of threads' priorities.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with thread priority