Vintage appMaker의 Tech Blog

[kotlin] Quick coroutine(코루틴) 예제 본문

Source code or Tip/Android(Java, Kotlin)

[kotlin] Quick coroutine(코루틴) 예제

VintageappMaker 2021. 3. 11. 20:31

Async 관련 coroutine, Flow

coroutine 필수핵심만 정리하기

Android의 동영상 플레이어(Exoplayer)에서 구간별 썸네일과 정보를 출력해야 한다면
비동기 상태로 여러작업을 동시에 해야한다. 그 때 kotlin을 사용한다면 코루틴은 선택이 아닌 필수가 된다. 
안정적이고 쉽게 관리가 가능하기 때문이다. 

 

  • 코루틴 ( coroutine )

    비동기 방식의 협력형 멀티태스킹. Thread보다 쉽고 가볍고 안정적임.

    • 적용방법

      • ( 1 ) 비동기 code가 적용되는 범위설정

        • CoroutineScope() 함수를 이용하여 어디에서 실행될 것인지 결정
          • Dispatchers.Main (Android의 UI Thread용)
          • Dispatchers.IO (File IO, Network IO)
          • Dispatchers.Default (background)
            val scope = CoroutineScope(Dispatchers.IO)
      • ( 2 ) 비동기 code 실행 - launch{ ... }

        • withContext로 Main, IO, Default 전환

          scope.launch {
            withContext(Dispatchers.Default){
                // 백그라운드 작업
                delay(2000)
                println ("Hi, background")
            }
          }
          • 코루틴 내의 비동기 함수 : async{} 결과값을 리턴하고 await()로 대기한다.

          • 코루틴 내의 대기함수 : delay(milliseconds)

          • 타임아웃으로 대기하고 기다림 : withTimeoutOrNull(milliseconds)

            scope.launch {
                // CoroutineContext 를 변경하여 백그라운드로 전환하여 작업을 처리합니다
                val rst = withTimeoutOrNull(10000){
                    delay(5000)
                    "data processing Success"
                }
            
                if (rst == null){
                    println("time out 1 sec")
                } else {
                   println(rst)
                }
            
            }
            

           

      • 참고: runBlocking ()은 Thread를 점유하므로 사용을 권장하지 않음.

          // thread를 혼자점유함. Android 프로그래밍에서는 피해야 하는 코드(ANR)
          runBlocking {
              // async를 통해 동시작업을 수행함.
              var process1 = async {
                  delay(1600);
                  println("process1")
                  100
              }
        
              var process2 = async {
                  delay(1000);
                  println("process2")
                  200
              }
        
              println(process2.await()+ process1.await())
              delay(15000)
          }
        
          // 메모리 누수방지 위해 cancel
          scope.cancel()
          println("end")

결과화면

> Task :CoroutinetestKt.main()
process2
process1
300
Hi, background
data processing Success
end


전체소스

 

VintageAppMaker/KotlinOnepointLesson

Kotlin을 빠르게 시작하기 위한 소스정리. Contribute to VintageAppMaker/KotlinOnepointLesson development by creating an account on GitHub.

github.com

 

 

Comments