Skip to content

Instantly share code, notes, and snippets.

@Pooh3Mobi
Last active January 17, 2019 15:52
Show Gist options
  • Save Pooh3Mobi/d63caaf2baa46c95d717ccd104506881 to your computer and use it in GitHub Desktop.
Save Pooh3Mobi/d63caaf2baa46c95d717ccd104506881 to your computer and use it in GitHub Desktop.
Kotlin Coroutines Sample 01
// A-D blocking thread
fun testA() {
println(1)
Thread.sleep(1000)
println(2)
Thread.sleep(1000)
println(3)
}
fun testB() {
runBlocking {
println(1)
GlobalScope.launch {
delay(1000)
println(2)
}.join()
delay(1000)
println(3)
}
}
fun testC() {
runBlocking {
println(1)
GlobalScope.async {
delay(1000)
println(2)
}.await()
delay(1000)
println(3)
}
}
fun testD() {
runBlocking {
println(1)
sampleSuspending()
delay(1000)
println(3)
}
}
suspend fun sampleSuspending() {
delay(1000)
println(2)
}
// E〜I loading sample in Activity And TextView
fun MainActivity.testE() {
GlobalScope.launch(Dispatchers.Main) {
textView.text = "start"
val result = GlobalScope.async {
Thread.sleep(1000);
1000
}.await()
textView.text = "waited: $result"
}
}
fun MainActivity.testF() {
GlobalScope.launch(Dispatchers.Main) {
textView.text = "start"
val result = slow().await()
textView.text = "waited: $result"
}
}
fun slow() = GlobalScope.async<Int> {
Thread.sleep(1000);
1000
}
fun MainActivity.testG() {
GlobalScope.launch(Dispatchers.Main) {
textView.text = "start"
val result = slow2()
textView.text = "waited: $result"
}
}
suspend fun slow2(): Int = coroutineScope {
return@coroutineScope async(Dispatchers.IO) { Thread.sleep(1000);1000 }.await()
}
fun MainActivity.testH() {
GlobalScope.launch(Dispatchers.Main) {
textView.text = "start"
val result = slow3()
textView.text = "waited: $result"
}
}
suspend fun slow3(): Int = coroutineScope {
val one = async { delay(1000L); 1000 }
one.await()
}
fun MainActivity.testI() {
GlobalScope.launch(Dispatchers.Main) {
textView.text = "start"
val result = slow4()
textView.text = "waited: $result"
}
}
suspend fun slow4(): Int = coroutineScope {
withContext(Dispatchers.Default) { delay(1000L); 1000 }
}
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.Single
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.rx2.await
import java.util.concurrent.TimeUnit
import kotlin.system.measureTimeMillis
class MainActivity : AppCompatActivity() {
@ExperimentalCoroutinesApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view ->
testE()
}
}
}
fun logThreadName(msg: String = "") = Log.d("test", "[$msg]:${Thread.currentThread().name}")
// A + B
fun MainActivity.testA() {
fun asyncA() = Single.just(10)
.delay(1000, TimeUnit.MILLISECONDS)
fun asyncB() = Single.just(20)
.delay(2000, TimeUnit.MILLISECONDS)
GlobalScope.launch(Main) {
val result = measureTimeMillis {
textView.text = "start"
val a = async(IO) { asyncA().await() }
val b = async(IO) { asyncB().await() }
val sum = a.await() + b.await()
textView.text = "sum: $sum"
}
Log.d("test", "time: $result")
}
}
fun MainActivity.testB() {
fun heavyA() = 10.also { Thread.sleep(1000) }
fun heavyB() = 20.also { Thread.sleep(2000) }
GlobalScope.launch(Main) {
val time = measureTimeMillis {
textView.text = "start"
val a = async(IO) { heavyA() }
val b = async(IO) { heavyB() }
val sum = a.await() + b.await()
textView.text = "sum: $sum"
}
Log.d("test", "time: $time")
}
}
// A + (B + C)
fun MainActivity.testC() {
fun heavyA() = 10.also { Thread.sleep(1000); logThreadName("A") }
fun heavyB() = 20.also { Thread.sleep(2000); logThreadName("B") }
fun heavyC() = 30.also { Thread.sleep(3000); logThreadName("C") }
GlobalScope.launch(Main) {
val time = measureTimeMillis {
textView.text = "start"
val sum = async(IO) {
val a = heavyA()
val ab = async {
val b = async { heavyB() }
val c = async { heavyC() }
b.await() + c.await()
}
a + ab.await()
}.await()
textView.text = "sum: $sum"
}
Log.d("test", "time2: $time")
}
}
fun MainActivity.testD() {
fun heavyA() = 10.also { Thread.sleep(1000); logThreadName("A") }
fun heavyB() = 20.also { Thread.sleep(2000); logThreadName("B") }
fun heavyC() = 30.also { Thread.sleep(3000); logThreadName("C") }
GlobalScope.launch(Main) {
val time = measureTimeMillis {
textView.text = "start"
val a = async(IO, start = CoroutineStart.LAZY) { heavyA() }
val bc = async(IO, start = CoroutineStart.LAZY) {
val b = async(IO) { heavyB() }
val c = async(IO) { heavyC() }
b.await() + c.await()
}
val sum = a.await() + bc.await()
textView.text = "sum: $sum"
}
Log.d("test", "time2: $time")
}
}
fun MainActivity.testE() {
GlobalScope.launch(Main) {
val time = measureTimeMillis {
textView.text = "start"
val sum = asyncSum().await()
textView.text = "sum: $sum"
}
Log.d("test", "time2: $time")
}
}
fun asyncSum() = GlobalScope.async(IO) {
fun heavyA() = 10.also { Thread.sleep(1000); logThreadName("A") }
fun heavyB() = 20.also { Thread.sleep(2000); logThreadName("B") }
fun heavyC() = 30.also { Thread.sleep(3000); logThreadName("C") }
val a = async(start = CoroutineStart.LAZY) { heavyA() }
val bc = async(start = CoroutineStart.LAZY) {
val b = async { heavyB() }
val c = async { heavyC() }
b.await() + c.await()
}
a.await() + bc.await()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment