• 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

Executor runnable priority

 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just now digging into the threading stuff introduced in java 1.5 (Executors and such). Is there a way to tell an executor that a runnable you pass it is more or less important than another?

Up till now I have been using the setPriority() method on my threads as I create them to allow more important threads to execute quicker when they are run by holding back on running the less important ones.

Thanks for your input.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like there isn't really a way to do this directly. One approach would be to have each task set its priority for itself as it runs, using a simple decorator:

This would work well enough to set a lower priority. However if you want to set a higher priority, beware that the thread won't acquire that higher priority until after it starts running (and completes the first three lines of the run() method). Which may be slightly unsatisfactory, but it's better than nothing.

Another approach would be to forget about priorities while a job is executing, and instead focus on the priorities of tasks waiting to start executing. You could use a PriorityBlockingQueue to manage a group of tasks waiting to start execution by a group of worker threads, e.g. from Executors.newFixedThreadPool(). Whenever the pool is available to add a new task (as evidenced by the execute() method not blocking), use the priority queue to determine which task to add. This solution is also imperfect - it does nothing to affect the relative priorities of tasks while they execute. Also you need to pick out a new task before calling execute(), which may block - so what you get is the highest-priority task that was in the queue when you first started waiting for an available thread. Not necessarily the highest-priority task later when the thread becomes available. So again it's not perfect, but better than nothing.

It's also possible to combine these two approaches, as well as to extend the wrapper idea to obtain the highest-priority task currently available. Though additional complications may emerge, and it starts to be more like building your own thread pool rather than using existing libraries. Could be I'm missing some easier way to go about this...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just brainstorming ... An executor per priority, set thread priority in the thread factory method? Hmmm, that could have exactly opposite the effect you want ... all the high priority stuff queued up waiting for a thread while the one low priority job gets serviced right away. Ok, an executor per priority with more threads in the high priority pool, fewer threads in the lower priority pool? An implementation of BlockingQueue that vends tasks in priority order, not FIFO? (We replaced JMS queues with a database in a workflow system so we could do priority order "get next"). Some of these ideas might depend on your load profile ... size of backlog, ratios of high, med, low priority, ratios of long and short running tasks, etc. In other words, I think I haven't a clue. Any of that useful anyhow?

Editing: I guess I didn't read all of Jim's the first time through ... I think I duplicated some.
[ October 26, 2006: Message edited by: Stan James ]
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I'll probably start by trying to use the plain vanila CachedThreadPool and see if I have any bottlenecking anywhere. If I have problems, I'll look into using the PriorityBlockingQueue to order the tasks.

Thanks for your suggestions.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Shepherd:
I'm just now digging into the threading stuff introduced in java 1.5 (Executors and such). Is there a way to tell an executor that a runnable you pass it is more or less important than another?

Up till now I have been using the setPriority() method on my threads as I create them to allow more important threads to execute quicker when they are run by holding back on running the less important ones.

Thanks for your input.



I realize this thread is old, but I was looking for an answer and Google found it, so I thought I'd put my solution here.

To set the thread priority for an Executor you write a ThreadFactory and set the priority of the threads there.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jon Strayer:


I realize this thread is old, but I was looking for an answer and Google found it, so I thought I'd put my solution here.

To set the thread priority for an Executor you write a ThreadFactory and set the priority of the threads there.



Which works if all the Threads/Runnables should have the same priority (or if you tweak it all the Runnables inside a given Thread run at the same priority). I think the OP was looking to have different priorities per Runnable, and since each Thread in the pool may execute multiple Runnables with different priorities this wouldn't be ideal.

Just to throw an extra bit of info, you could:
1) Use a PriorityBlockingQueue to make sure high-priority tasks start first
2) To control the Runnables CPU priority you could subclass ThreadPoolExecutor and provide implementations of the beforeExecute method to set the Runnable's Thread priority before the Runnable executes, then use the afterExecute to re-set the Thread priority to default.

I think this would take care of both ensuring high-priority tasks start before low priority tasks, and make sure that high-priority tasks get more CPU time than low-priority tasks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic