I have the following situation: job1 and job2 go to the server in the same time and both of them came back with status 401
, which means that my token access has expired and I need to make a refresh. I start job3 which came back the new token. In this case, I have to recreate the job1 and job2 with the new token on request and start them.
I have a jobDispatcher, but it seems that it not help me in situation. Here it is :
class JobDispatcher : CoroutineDispatcher() {
private val queue: Queue<Runnable> = LinkedList()
private var isPaused: Boolean = false
private var lastExecutedBlock: Runnable? = null
@Synchronized
override fun dispatch(context: CoroutineContext, block: Runnable) {
if (isPaused) {
queue.add(block)
} else {
thread {
lastExecutedBlock = block
block.run()
}
}
}
@Synchronized
fun pause() {
isPaused = true
if (lastExecutedBlock != null) {
queue.add(lastExecutedBlock)
lastExecutedBlock = null
}
}
@Synchronized
fun resume() {
isPaused = false
runQueue()
}
}
Pause method is called before job3 to start, and when the result is successful, resume method is called. The problem is that job1 and job2 are now completed and now I should recreate them. Is there any possibility to clone the job, and put it in a queue?
My question is: which is the best solution to solve this? I fell a little lost when working with coroutines. My code is much complicated then I described here, I need just some guidelines for this situation, how to manage it. How to communicate between coroutines?
Any idea is welcome. Thank you!