https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

Sourabh Rawat

07/02/2021, 2:52 AM
I have this code
Copy code
class Executor {
    //    private val scope = CoroutineScope(Job() + Executors.newFixedThreadPool(10).asCoroutineDispatcher())
    private val context = Executors.newFixedThreadPool(10).asCoroutineDispatcher()

    suspend fun submit(value: Any) {
        withContext(context) {
            logger.debug { "Successfully received: $value" }
        }
    }

    fun stop() {
        // TODO
    }
}
How can I cancel the submitted jobs? And fail submission of a new job if the context is "cancelled"
d

Dominaezzz

07/02/2021, 5:22 AM
By cancelling that job object
s

Sourabh Rawat

07/02/2021, 7:12 AM
I want to bulk cancel. Meaning I'll be calling submit multiple times.
u

uli

07/02/2021, 9:00 AM
Copy code
class Executor {
    //    private val scope = CoroutineScope(Job() + Executors.newFixedThreadPool(10).asCoroutineDispatcher())
    private val dispatcher = Executors.newFixedThreadPool(10).asCoroutineDispatcher()
   private val scope = CoroutineScope(Job() + dispatcher)

    fun submit(value: Any) {
        scope.launch {
            logger.debug { "Successfully received: $value" }
        }
    }

    fun stop() {
        scope.cancel()
    }
}
2 Views