0

I want to write a thread pool that is able to process Runnables. The execution time of those runnables ist not predictable, but the responsitivity should be as high as possible. To achieve this I want to monitor if there are Runnables that need very long to execute. Those Runnables are considered not as important for resposnitivity as other ones, so if there are such, I want to be able to pause them, in order to execute another, shorter Runnable on the Thread where the more advanced Runnable was executed before and after that resume the execution of the longer lasting Runnable. Is it possible to Pause a Runnable, and save its current state in Java, so I can achieve this?

th3falc0n
  • 1,389
  • 1
  • 12
  • 33
  • duplicate? http://stackoverflow.com/questions/11989589/how-to-pause-and-resume-a-thread-in-java-from-another-thread – Jeffrey Blattman Jan 07 '15 at 18:29
  • 1
    If you really (really) need them to be running on the same thread, it's easy have the bigger tasks call Thread.yield() once in a while, allowing the other tasks to have a chance to complete. The point of thread pools though, is to have tasks running on different threads... – Johan Tidén Jan 07 '15 at 18:38
  • @JohanTidén - Uh, `Thread.yield` causes the thread to (potentially) yield it's *processor* to other threads. It doesn't allow other "tasks" to run on the same thread. – Hot Licks Jan 07 '15 at 19:02
  • You cannot move an active workload between Threads. Once you've instructed a Thread to `start` or `run`, it will continue on that piece of work (eg, a Runnable) until the work is completed or somehow "terminated". – Hot Licks Jan 07 '15 at 19:08
  • "...I want to be able to pause them..." No, you don't. Just ask yourself, what happens if the thread that you paused is, at that exact moment, the owner of a lock? – Solomon Slow Jan 07 '15 at 19:52

2 Answers2

1

I'd let the OS worry about thread scheduling... it will do it far better than you will ever be able to (it knows a lot more about what is going on than a higher level program can)

Here is a nice example of a Task Engine from an Open Source project (the very popular OpenFire project)

https://github.com/igniterealtime/Openfire/blob/master/src/java/org/jivesoftware/util/TaskEngine.java

This engine will allow you to schedule tasks with various delays and other controls for executing long running tasks. Internally it uses ExecutorService with a pool of available threads of which your different tasks will execute on.

SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
0

Thanks to your advices I came to an alternative solution. I will write my own Thread-Pool, consisting of an live pool, which has an limited amount of parallel threads that will execute all incoming tasks. This threads will advance to another pool, consisting of all threads that need to execute a longer lasting task, if they start blocking the pool with those tasks. This way all fast actions will remain in the thread pool, while more advanced task will get their own threads.

th3falc0n
  • 1,389
  • 1
  • 12
  • 33