Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Threads and Synchronization and the fly likes Set Thread priorities based on certain conditions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Set Thread priorities based on certain conditions" Watch "Set Thread priorities based on certain conditions" New topic
Author

Set Thread priorities based on certain conditions

Sandeep Kumar B
Ranch Hand

Joined: Jul 10, 2011
Posts: 41
I read that thread priorities may not ensure that your program would run with the same path as the thread priorities
But in the long run(with time), priorities does take effect?

I read this thread

http://www.coderanch.com/t/541462/threads/java/why-output-so-different#2456564

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.



I wrote this code


When I see the output its different everytime(even if we ignore the if loops)
Now my question is lets say a scenario
where three threads are downloading different files of size 2, 8 and 20 MB
I want the logic that the thread downloading the smallest file should get more priority
Is above logic the right way to implement the same?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set Thread priorities based on certain conditions
 
Similar Threads
Thread's Priority
doubt abt priorities in threads.......
Joining Thread, Explanation required
Thread Priority
Thread doubt