aspose file tools*
The moose likes Threads and Synchronization and the fly likes Executor runnable priority Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Executor runnable priority" Watch "Executor runnable priority" New topic
Author

Executor runnable priority

Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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...


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
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.
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
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.


Jon
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3968
    
  17

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.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executor runnable priority
 
Similar Threads
Thread question
Callable and executor Framework
Try to stop execution of multiple threads if found a result from one of the thread.
ExecutorService invokeAll - blocking?
Comparison between Threads-based scheduling and Timer-based scheduling in Java Thread