Skip to content

Commit

Permalink
Added coroutine activity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramakrishna Joshi committed Sep 21, 2022
1 parent 63c6c73 commit dd4f73d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
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 app/src/main/res/layout/activity_understanding_coroutine.xml
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>

0 comments on commit dd4f73d

Please sign in to comment.