'Pause and Resume Thread
Say we have a thread processing certain instructions and hence creating intermediary result objects, is there any way to pause this thread and later create a new thread to resume from where the previous thread has stopped?
Arbitrary Example:
Result result1 = foo();
Result result2 = fooAgain();
if(someCondition){
// save everything thats been computed so far and release this thread
}
Result result3 = notFoo();
// end of the method
// in a quartz scheduler or something
if(someCondition2){
// signal to resume the execution, but not from scratch instead from where its paused
}
so my question is how do i achieve this and also whats the best way to save the intermediary results
Solution 1:[1]
It sounds like you shouldn't be pausing anything. Instead have one set of threads creating the intermediate results and saving them somewhere. Then have another set of threads that find these intermediate results and do whatever with them.
There isn't one right answer about how to save them in general, it depends on the context. But it would make sense to use some kind of queue for this.
This producer-consumer setup can be seen as a way for a thread to resume a task begun by another thread. The intermediate result can include information to indicate what step the task needs to be begun from. The queue's contents can be saved so that they can be restored after bringing the system down and back up so that pending items can get completed.
I have seen what happens when using a scheduler to progress tasks. The system alternates between idly wasting cycles to overloading on work. When the timer fires it throws new work at the system regardless of whether there is capacity. Using Queues instead allows back pressure to prevent overload, and lets the threads work at a steady pace.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
