th3falc0n
th3falc0n

Reputation: 1427

Pause executing Runnable and/or move it to another Thread

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?

Upvotes: 0

Views: 754

Answers (2)

th3falc0n
th3falc0n

Reputation: 1427

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.

Upvotes: 0

SnakeDoc
SnakeDoc

Reputation: 14361

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.

Upvotes: 1

Related Questions