-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ramakrishna Joshi
committed
Sep 21, 2022
1 parent
63c6c73
commit dd4f73d
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/example/unittestinginandroid/playground/SuspendingCoroutines.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.unittestinginandroid.playground | ||
|
||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.runBlocking | ||
|
||
fun main() { | ||
runBlocking { | ||
println("main started") | ||
|
||
joinAll( | ||
async { coroutine(1, 500) }, | ||
async { coroutine(2, 300) }, | ||
async { | ||
repeat(10) { | ||
println("Other task ${it+1} executed") | ||
delay(100) | ||
} | ||
} | ||
) | ||
|
||
|
||
println("main ended") | ||
} | ||
} | ||
|
||
suspend fun coroutine(number: Int, time: Long) { | ||
println("Coroutine $number started") | ||
delay(time) | ||
println("Coroutine $number ended") | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/res/layout/activity_understanding_coroutine.xml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".playground.UnderstandingCoroutineActivity"> | ||
|
||
<Button | ||
android:id="@+id/buttonLaunchCoroutine" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Launch Coroutine" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintBottom_toBottomOf="parent"/> | ||
|
||
|
||
<Button | ||
android:id="@+id/buttonRunBlocking" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Launch RunBlocking" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/buttonLaunchCoroutine" | ||
/> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |