Android Jetpack

Coroutine Simple Example

----___<<<<< 2022. 4. 11. 00:35

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        SimpleThread1().start()
//        SimpleThread2().start()
        start()
    }

}

fun start() = runBlocking {

    test1()
    test2()

//    SimpleThread1().start()
//    SimpleThread2().start()

}

suspend fun test1() = coroutineScope {


    Log.d("test1 : ", "p1")

    delay(1000)

    Log.d("test1 : ", "p2")

    delay(1000)

}

suspend fun test2() = coroutineScope{

    Log.d("test2 : ", "p3")

    delay(1000)

    Log.d("test2 : ", "p4")

    delay(1000)

}

class SimpleThread1 : Thread() {

    override fun run() {

        Log.d("SimpleThread1", "p1")

        sleep(1000)

        Log.d("SimpleThread1", "p2")

        sleep(1000)

    }
}

class SimpleThread2 : Thread() {

    override fun run() {

        Log.d("SimpleThread2", "p3")

        sleep(1000)

        Log.d("SimpleThread2", "p4")

        sleep(1000)

    }
}

'Android Jetpack' 카테고리의 다른 글

Retrofit이란?  (0) 2022.04.12
ViewModelScope  (0) 2022.04.11
What is Coroutine?  (0) 2022.04.10
Android LiveData - 6 (ViewModel + LiveData + DataBinding)  (0) 2022.04.03
Android View에 대한 접근 - 6 (Adapter DataBinding)  (0) 2022.03.09