This file contains hidden or 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
| @ExperimentalCoroutinesApi | |
| suspend fun makeSomeThirdPartySdkCall() { | |
| Log.d("makeThirdPartySdkCall", "STARTING EXECUTION") | |
| var result: String? = "lol" | |
| result = consumeResponseSuspendedWrapper { apiCompletionCallback: APICompletionCallback -> | |
| sdkFunction(apiCompletionCallback) | |
| } | |
| Log.d("makeThirdPartySdkCall", "ENDING EXECUTION $result") | |
| } |
This file contains hidden or 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
| suspend fun func1(){ | |
| Log.d("SUSPENDED: 1", "STARTING EXECUTION") | |
| val result = coroutineScope { | |
| awaitAll( | |
| async { | |
| func2() | |
| }, | |
| async { | |
| func3() | |
| }, |
This file contains hidden or 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
| @ExperimentalCoroutinesApi | |
| suspend fun makeSomeThirdPartySdkCall() { | |
| Log.d("makeHyperVergeFaceMatch", "STARTING EXECUTION") | |
| var result: String? = "lol" | |
| result = consumeResponseSuspendedWrapper { apiCompletionCallback: APICompletionCallback -> | |
| sdkFunction(apiCompletionCallback) | |
| } | |
| Log.d("makeHyperVergeFaceMatch", "ENDING EXECUTION $result") | |
| } |
This file contains hidden or 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
| suspend fun funcA(): Int{ | |
| Log.d("SUSPENDED: A", "STARTING EXECUTION") | |
| //suspension point or label | |
| funcB() | |
| //suspension point or label | |
| funcC() | |
| Log.d("SUSPENDED: A", "ENDING EXECUTION") | |
| return 4 | |
| } |
This file contains hidden or 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
| @Nullable | |
| public final Object func2(@NotNull Continuation var1) { | |
| Object $continuation; | |
| label20: { | |
| if (var1 instanceof <undefinedtype>) { | |
| $continuation = (<undefinedtype>)var1; | |
| if ((((<undefinedtype>)$continuation).label & Integer.MIN_VALUE) != 0) { | |
| ((<undefinedtype>)$continuation).label -= Integer.MIN_VALUE; | |
| break label20; | |
| } |
This file contains hidden or 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
| suspend fun func2(): Int{ | |
| Log.d("SUSPENDED: 2", "STARTING EXECUTION") | |
| delay(3000) | |
| Log.d("SUSPENDED: 2", "ENDING EXECUTION") | |
| return 2 | |
| } |
This file contains hidden or 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
| private suspend fun launch100000Coroutines(){ | |
| val time = measureTimeMillis { | |
| runBlocking { | |
| for(i in 1..10000) { | |
| launch { | |
| delay(10L) | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| private fun launch10000Threads(){ | |
| val time = measureTimeMillis { | |
| for(i in 1..10000) { | |
| Thread(Runnable { | |
| Thread.sleep(10) | |
| }).run() | |
| } | |
| } | |
| println("Took $time ms to complete 10000 iterations using threads each taking ~10ms") | |
| } |
This file contains hidden or 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
| /** | |
| * [CoroutineScope] tied to this [ViewModel]. | |
| * This scope will be canceled when ViewModel will be cleared, i.e [ViewModel.onCleared] is called | |
| * | |
| * This scope is bound to | |
| * [Dispatchers.Main.immediate][kotlinx.coroutines.MainCoroutineDispatcher.immediate] | |
| */ | |
| public val ViewModel.viewModelScope: CoroutineScope | |
| get() { | |
| val scope: CoroutineScope? = this.getTag(JOB_KEY) |
This file contains hidden or 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
| /** | |
| * A global [CoroutineScope] not bound to any job. | |
| * Global scope is used to launch top-level coroutines which are operating on the whole application lifetime | |
| * and are not cancelled prematurely. | |
| * | |
| * Active coroutines launched in `GlobalScope` do not keep the process alive. They are like daemon threads. | |
| * | |
| * This is a **delicate** API. It is easy to accidentally create resource or memory leaks when | |
| * `GlobalScope` is used. A coroutine launched in `GlobalScope` is not subject to the principle of structured | |
| * concurrency, so if it hangs or gets delayed due to a problem (e.g. due to a slow network), it will stay working |
NewerOlder