본문 바로가기
[coroutine] Coroutine Basics ( 코루틴 기초 ) https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/basics.md#coroutine-basics Your first coroutine - fun main(){ GlobalScope.launch{ delay(1000L) println("World!") } println("Hello,") Thread.sleep(2000L) } // Hello, // World! - 기본적으로 코루틴은 light-weight thread 이다. CoroutineScope 라는 context 안에서 "launch" 와 같은 coroutine builder 를 통해 시작된다. 위의 예제에서는 GlobalScope 라는 coroutine 을 사용해서 launch 를 했.. 2020. 3. 7.
[coroutine] exception 을 이해해보자 #2 ( in global scope ) -[coroutine] exception 을 이해해보자 #1 ( in my coroutine scope ) 과 동일한 sample 들을 직접 생성한 coroutine 이 아닌 GlobalScope 에서 수행해보기로 했다.혹시나 뭔가 동작이 다를까 해서..내가 알고 있기로는 GlobalScope 은 defaultHandler 가 setting 되어 결국 모든 exception 을 먹어 crash 가 나지 않는다는 차이만 있는것으로 알고 있는데.. 검증해보자 ( 결론은 잘못 알고 있었다. ) -class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState).. 2019. 3. 6.
[coroutine] exception 을 이해해보자 #1 ( in my coroutine scope ) -이 글은 coroutine exception 에 대한 일종의 심화과정이라고 볼 수 있다.기본 exception 에 대한 내용을 먼저 이해한 후 이곳의 글을 보자![coroutine] Exception handling -class MainActivity : Activity() { private val coroutineScope = CoroutineScope(Dispatchers.Main) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) findViewById(R.id.btn).setOnClickListener { try { co.. 2019. 3. 5.
[coroutine] Exception handling Exception propagation - Coroutine builder 는 2가지 flavor 가 있다. exception 을 자동으로 전파하는 launch, actor류. 그리고 user 에게 노출되는 async, produce 류. 자동 전파하는 케이스는 Java 의 uncaughtExceptionHandler 와 비슷하게 unhandled exception 이다. 반면에 user 에게 노출되는 경우는 user 가 exception 을 직접 handle 할 수 있다. (돼왕: try-catch 할 수 있다.) 대표적 api 는 await 와 receive 가 있다. - runBlocking{ val job = GlobalScope.launch{ println(“Throwing exception f.. 2019. 3. 4.
[Coroutine] Coroutine context and dispatchers https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutine-context-and-dispatchers.md#thread-local-data -기존에 있었던 doc 이 update 되어서 따로 정리한다. Coroutine context and dispatchers -Coroutine 은 항상 어떤 context 하에서 실행된다.이 context 는 CoroutineContext type 을 가지며 Kotlin standard lib 에 정의되어 있다. -Coroutine context 는 여러개의 element 들을 가진 set 이다.main element 는 coroutine 의 Job, dispatcher 이다. Dispatchers.. 2019. 2. 8.
반응형