Skip to content

Instantly share code, notes, and snippets.

@thiyagu06
Created December 31, 2019 10:56
Show Gist options
  • Save thiyagu06/9523339fd365017b7eb6051677df8416 to your computer and use it in GitHub Desktop.
Save thiyagu06/9523339fd365017b7eb6051677df8416 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.concurrent.Executors
import kotlin.coroutines.CoroutineContext
class LearnScope {
private val supervisorJob = SupervisorJob()
val fastProcessingContext: CoroutineContext = Executors.newFixedThreadPool(1).asCoroutineDispatcher()
private val scope = CoroutineScope(supervisorJob + fastProcessingContext)
val slowProcessingContext: CoroutineContext = Executors.newFixedThreadPool(6).asCoroutineDispatcher()
fun startProcssing() {
scope.launch {
with(fastProcessingContext) {
delay(500) //pretend fast processing I/O
}
with(slowProcessingContext) {
delay(1000)
}
}
}
fun stopProcessing() {
supervisorJob.cancel()
}
}
fun main() = runBlocking{
val test = LearnScope()
test.startProcssing()
delay(5000)
test.stopProcessing()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment