In my example I have:
Task - does some long running operations in background thread: looping over large data set and do some other heavy operations for each item.
TaskExecutor - put Task to execute in background Thread from ThreadPool.
Caller - client requests TaskExecutor to start, pause and resume Task at some point.
class TaskExecutor {
val threadPool = Executors.newFixedThreadPool(1) as ThreadPoolExecutor
// begin API methods section
fun submitTask() {
threadPool.submit(Task())
}
fun pauseThreadPool() {
// pause all threads (in this example just one thread) in threadPool
}
fun resumeThreadPool() {
// resume all threads (in this example just one thread) in threadPool
}
// end API methods section
fun runTask() {
for(item in items) {
// do something with item
}
}
inner class Task(): Runnable {
override fun run() {
runTask()
}
}
}
class Caller() {
val taskExecutor = TaskExecutor()
taskExecutor.submitTask()
...
...
taskExecutor.pauseThreadPool()
...
...
taskExecutor.resumeThreadPool()
}
Question: is there any possibility to pause ThreadPool(or Thread from ThreadPool) and resume it by Caller to call pauseThreadPool() and resumeThreadPool() functions when it needs. Expected time for ThreadPool to be on pause is up to a few minutes.