Created
December 31, 2019 10:56
-
-
Save thiyagu06/9523339fd365017b7eb6051677df8416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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