-
-
Save gmk57/113b930588b51159c2bed8deb1ade1ca to your computer and use it in GitHub Desktop.
Testing recursive suspend functions
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
package gmk57.testrecursive | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.lifecycle.lifecycleScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.yield | |
import kotlin.concurrent.thread | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
thread { testStackDepth1(1) } | |
lifecycleScope.launch(Dispatchers.IO) { testStackDepth2(1) } | |
lifecycleScope.launch(Dispatchers.IO) { testStackDepth3(1) } | |
// lifecycleScope.launch(Dispatchers.IO) { testStackDepth4(1) } | |
} | |
private fun testStackDepth1(count: Int) { | |
if (count % 1000 == 0) println("testStackDepth1: $count") | |
try { | |
testStackDepth1(count + 1) | |
} catch (e: StackOverflowError) { | |
println("testStackDepth1 max: $count") | |
} | |
} | |
private suspend fun testStackDepth2(count: Int) { | |
if (count % 1000 == 0) println("testStackDepth2: $count") | |
try { | |
testStackDepth2(count + 1) | |
} catch (e: StackOverflowError) { | |
println("testStackDepth2 max: $count") | |
} | |
} | |
private suspend fun testStackDepth3(count: Int) { | |
if (count % 1000 == 0) println("testStackDepth3: $count") | |
try { | |
yield() | |
testStackDepth3(count + 1) | |
} catch (e: StackOverflowError) { | |
println("testStackDepth3 max: $count") | |
} | |
} | |
private suspend fun testStackDepth4(count: Int) { | |
if (count % 1000 == 0) println("testStackDepth4: $count") | |
try { | |
delay(1) | |
testStackDepth4(count + 1) | |
} catch (e: StackOverflowError) { | |
println("testStackDepth4 max: $count") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment