CoroutineContext
: ์ฝ๋ฃจํด์ ์คํํ๋ ์คํ ํ๊ฒฝ์ ์ค์ ํ๊ณ ๊ด๋ฆฌํ๋ ์ธํฐํ์ด์ค
Coroutine Context ๊ตฌ์ฑ ์์
CoroutineName
: ์ฝ๋ฃจํด ์ด๋ฆ ์ค์ CoroutineDispatcher
: ์ฝ๋ฃจํด์ ์ค๋ ๋์ ํ ๋นํด ์คํJob
: ์ฝ๋ฃจํด์ ์ถ์์ฒด๋ก ์ฝ๋ฃจํด์ ์กฐ์CoroutineExceptionHandler
: ์ฝ๋ฃจํด์์ ๋ฐ์ํ ์์ธ ์ฒ๋ฆฌ
Coroutine Context ๊ตฌ์ฑ
Coroutine Context ๊ตฌ์ฑ
- ํค - ๊ฐ ์์ผ๋ก ๊ตฌ์ฑ ์์๋ฅผ ๊ด๋ฆฌ
CoroutineContext
๊ฐ์ฒด ๊ฐ์ ๋ํ๊ธฐ ์ฐ์ฐ์(+)๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑ
val coroutineContext: CoroutineContext = newSingleThreadContext("MyThread") + CoroutineName("MyCoroutine")
Key | Value |
CoroutineName | CoroutineName(โMyCoroutineโ) |
CoroutineDispatcher | newSingleThreadContext("MyThread") |
Job | ์ค์ ๋์ง ์์ |
CoroutineExceptionHandler | ์ค์ ๋์ง ์์ |
- ์ค์ ๋์ง ์์ ๊ตฌ์ฑ ์์๋ ์ค์ ๋์ง ์์ ์ํ๋ก ์ ์ง!
๐ก EmptyCoroutineContext: ๊ตฌ์ฑ ์์๊ฐ ์๋ CoroutineContext
val emptyCoroutineContext: CoroutineContext = EmptyCoroutineContext
๊ตฌ์ฑ ์์ ๋ฎ์ด์ฐ๊ธฐ
- ๊ฐ์ ๊ตฌ์ฑ ์์๊ฐ ๋ ์ด์ ๋ํด์ง๋ค๋ฉด ๋์ค์ ์ถ๊ฐ๋ CoroutineContext ๊ตฌ์ฑ ์์๊ฐ ์ด์ ์ ๊ฐ์ ๋ฎ์ด ์์
- ์ค๋ณต๋์ง ์๋ ๊ตฌ์ฑ ์์๊ฐ ์์ ๊ฒฝ์ฐ ๋ฎ์ด์์์ง์ง ์๊ณ ๋จ์
fun main() = runBlocking<Unit> {
val coroutineContext: CoroutineContext = newSingleThreadContext("MyThread") + CoroutineName("MyCoroutine")
val newCoroutineContext: CoroutineContext = coroutineContext + CoroutineName("NewCoroutine")
launch(context = newCoroutineContext) {
// CoroutineName: NewCoroutine
}
}
CoroutineContext ๊ตฌ์ฑ ์์ ์ ๊ทผ
๊ตฌ์ฑ ์์์ ํค
CoroutineContext
๊ตฌ์ฑ ์์๋ ์์ ์ ๋ด๋ถ์ ํค๋ฅผ ์ฑ๊ธํค ๊ฐ์ฒด๋ก ๊ตฌํCoroutineContext.Key<T>
๋ฅผ ๊ตฌํํ๋companion object
Key
๋ฅผ ๊ฐ์ง
public interface Job : CoroutineContext.Element {
public companion object Key: CoroutineContext.Key<Job>
}
CoroutineContext ๊ตฌ์ฑ ์์ | ๊ตฌ์ฑ ์์ ํค |
CoroutineName | CoroutineName.Key |
Job | Job.Key |
CoroutineDispatcher | CoroutineDispatcher.Key |
CoroutineExceptionHandler | CoroutineExceptionHandler.Key |
- ์ฝ๋ฃจํด ๊ตฌ์ฑ ์์์ ํค๋ ๊ณ ์ ํจ โ ๋ชจ๋ ๊ฐ์ ํค๋ฅผ ๋ฐํ
Dispatcher.IO.key == Dispatcher.Default.key
ํค๋ฅผ ์ฌ์ฉํด ๊ตฌ์ฑ ์์์ ์ ๊ทผํ๊ธฐ
์ฑ๊ธํค ํค ์ฌ์ฉ
fun main() = runBlocking<Unit> {
val coroutineContext: CoroutineContext = CoroutineName("MyCoroutine") + Dispatcher.IO
val nameFromContext = coroutineContext[CoroutineName.Key]
println(nameFromContext) // CoroutineName(MyCoroutine)
}
coroutineContext
์ ๋ํด ์ฐ์ฐ์ ํจ์get()
์ ์ธ์๋กCoroutineName.Key
๋ฅผ ๋๊น
๊ตฌ์ฑ ์์ ์์ฒด๋ฅผ ํค๋ก ์ฌ์ฉ
CoroutineContext
์ ์ฃผ์ ๊ตฌ์ฑ ์์๋ค์companion object
๋กCoroutineContext.Key
๋ฅผ ๊ตฌํํ๋ Key๋ฅผ ๊ฐ๊ณ ์์
โ Key๋ฅผ ๋ช ์์ ์ผ๋ก ์ฌ์ฉํ์ง ์๊ณ ๊ตฌ์ฑ ์์ ์์ฒด๋ฅผ ํค๋ก ์ฌ์ฉ ๊ฐ๋ฅ
fun main() = runBlocking<Unit> {
val coroutineContext: CoroutineContext = CoroutineName("MyCoroutine") + Dispatcher.IO
val nameFromContext = coroutineContext[CoroutineName]
println(nameFromContext) // CoroutineName(MyCoroutine)
}
๊ตฌ์ฑ ์์์ key ํ๋กํผํฐ๋ฅผ ์ฌ์ฉ
- CoroutineContext์ ์ฃผ์ ๊ตฌ์ฑ ์์๋ค์ ๋ชจ๋ key ํ๋กํผํฐ๋ฅผ ๊ฐ์ง
- ์ด๋
companion object
๋ก ์ ์ธ๋ Key ์ ๋์ผํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด
- ์ด๋
fun main() = runBlocking<Unit> {
val coroutineName = CoroutineName("MyCoroutine")
val dispatcher = Dispatcher.IO
val coroutineContext = coroutineName + dispatcher
println(coroutineContext[coroutineName.key]) // CoroutineName("MyCoroutine")
println(coroutineContext[dispatcher.key]) // Dispatcher.IO
}
http://www.acornpub.co.kr/book/kotlin-coroutines
์ฝํ๋ฆฐ ์ฝ๋ฃจํด์ ์ ์
๋ง์ ๊ฐ๋ฐ์๋ค์ด ์ด๋ ต๊ฒ ๋๋ผ๋ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ๋ค์ํ ์๊ฐ์ ์๋ฃ์ ์ค๋ช ์ ํตํด ๋๊ตฌ๋ ์ฝ๊ฒ ์ดํดํ ์ ์๋๋ก ์ฐ์ธ ์ฑ ์ด๋ค.
www.acornpub.co.kr
๐ํ๊ธฐ๐
Spring Boot - Kotlin ์ฌ์ฉํ๋ ํ๊ฒฝ์์ ์ฝ๋ฃจํด์ ๋ํ ๋ ๊น์ ์ง์์ด ํ์ํด ์ฐพ๋ค ๋ฐ๊ฒฌํ ์ฑ .
ํ๊ตญ์ธ ์ ์ ๋ถ์ด์ด์ ๊ทธ๋ฐ์ง ์ ๋ฐ์ ์ผ๋ก ์ดํดํ๊ธฐ ๊ต์ฅํ ์ฝ๊ฒ ๋์ด์๊ณ ,
๊ธฐ์กด ์ฝํ๋ฆฐ ์ฑ ์์๋ ์ฝ๊ฒ ๋ค๋ค์ง์ง ์๋ ๋ ๋ฅํ ์ฝ๋ฃจํด ๊ธฐ๋ฅ๊น์ง ์ ์ ์์.
(์ง๊ธ๊น์ง ์ฝ์ ๊ธฐ์ ์ ์ค ์ค๋ฌด์์ ๊ฐ์ฅ ์ ์ฐ๊ณ ์๋ ์ฑ ...๊ฐ์ฅ ์ ์ ์ฝํ ์ฑ ...๐)
SpringBoot-Kotlin ํ๊ฒฝ์์ ์ฝ๋ฃจํด์ ๋ ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์ ๋ง ๊ฐ์ถ ํ์ ํ์ ๐๐ฅ๐๐ฅ๐๐ฅ๐๐ฅ๐๐ฅ
'๐ Study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
CoroutineContext
: ์ฝ๋ฃจํด์ ์คํํ๋ ์คํ ํ๊ฒฝ์ ์ค์ ํ๊ณ ๊ด๋ฆฌํ๋ ์ธํฐํ์ด์ค
Coroutine Context ๊ตฌ์ฑ ์์
CoroutineName
: ์ฝ๋ฃจํด ์ด๋ฆ ์ค์ CoroutineDispatcher
: ์ฝ๋ฃจํด์ ์ค๋ ๋์ ํ ๋นํด ์คํJob
: ์ฝ๋ฃจํด์ ์ถ์์ฒด๋ก ์ฝ๋ฃจํด์ ์กฐ์CoroutineExceptionHandler
: ์ฝ๋ฃจํด์์ ๋ฐ์ํ ์์ธ ์ฒ๋ฆฌ
Coroutine Context ๊ตฌ์ฑ
Coroutine Context ๊ตฌ์ฑ
- ํค - ๊ฐ ์์ผ๋ก ๊ตฌ์ฑ ์์๋ฅผ ๊ด๋ฆฌ
CoroutineContext
๊ฐ์ฒด ๊ฐ์ ๋ํ๊ธฐ ์ฐ์ฐ์(+)๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑ
val coroutineContext: CoroutineContext = newSingleThreadContext("MyThread") + CoroutineName("MyCoroutine")
Key | Value |
CoroutineName | CoroutineName(โMyCoroutineโ) |
CoroutineDispatcher | newSingleThreadContext("MyThread") |
Job | ์ค์ ๋์ง ์์ |
CoroutineExceptionHandler | ์ค์ ๋์ง ์์ |
- ์ค์ ๋์ง ์์ ๊ตฌ์ฑ ์์๋ ์ค์ ๋์ง ์์ ์ํ๋ก ์ ์ง!
๐ก EmptyCoroutineContext: ๊ตฌ์ฑ ์์๊ฐ ์๋ CoroutineContext
val emptyCoroutineContext: CoroutineContext = EmptyCoroutineContext
๊ตฌ์ฑ ์์ ๋ฎ์ด์ฐ๊ธฐ
- ๊ฐ์ ๊ตฌ์ฑ ์์๊ฐ ๋ ์ด์ ๋ํด์ง๋ค๋ฉด ๋์ค์ ์ถ๊ฐ๋ CoroutineContext ๊ตฌ์ฑ ์์๊ฐ ์ด์ ์ ๊ฐ์ ๋ฎ์ด ์์
- ์ค๋ณต๋์ง ์๋ ๊ตฌ์ฑ ์์๊ฐ ์์ ๊ฒฝ์ฐ ๋ฎ์ด์์์ง์ง ์๊ณ ๋จ์
fun main() = runBlocking<Unit> {
val coroutineContext: CoroutineContext = newSingleThreadContext("MyThread") + CoroutineName("MyCoroutine")
val newCoroutineContext: CoroutineContext = coroutineContext + CoroutineName("NewCoroutine")
launch(context = newCoroutineContext) {
// CoroutineName: NewCoroutine
}
}
CoroutineContext ๊ตฌ์ฑ ์์ ์ ๊ทผ
๊ตฌ์ฑ ์์์ ํค
CoroutineContext
๊ตฌ์ฑ ์์๋ ์์ ์ ๋ด๋ถ์ ํค๋ฅผ ์ฑ๊ธํค ๊ฐ์ฒด๋ก ๊ตฌํCoroutineContext.Key<T>
๋ฅผ ๊ตฌํํ๋companion object
Key
๋ฅผ ๊ฐ์ง
public interface Job : CoroutineContext.Element {
public companion object Key: CoroutineContext.Key<Job>
}
CoroutineContext ๊ตฌ์ฑ ์์ | ๊ตฌ์ฑ ์์ ํค |
CoroutineName | CoroutineName.Key |
Job | Job.Key |
CoroutineDispatcher | CoroutineDispatcher.Key |
CoroutineExceptionHandler | CoroutineExceptionHandler.Key |
- ์ฝ๋ฃจํด ๊ตฌ์ฑ ์์์ ํค๋ ๊ณ ์ ํจ โ ๋ชจ๋ ๊ฐ์ ํค๋ฅผ ๋ฐํ
Dispatcher.IO.key == Dispatcher.Default.key
ํค๋ฅผ ์ฌ์ฉํด ๊ตฌ์ฑ ์์์ ์ ๊ทผํ๊ธฐ
์ฑ๊ธํค ํค ์ฌ์ฉ
fun main() = runBlocking<Unit> {
val coroutineContext: CoroutineContext = CoroutineName("MyCoroutine") + Dispatcher.IO
val nameFromContext = coroutineContext[CoroutineName.Key]
println(nameFromContext) // CoroutineName(MyCoroutine)
}
coroutineContext
์ ๋ํด ์ฐ์ฐ์ ํจ์get()
์ ์ธ์๋กCoroutineName.Key
๋ฅผ ๋๊น
๊ตฌ์ฑ ์์ ์์ฒด๋ฅผ ํค๋ก ์ฌ์ฉ
CoroutineContext
์ ์ฃผ์ ๊ตฌ์ฑ ์์๋ค์companion object
๋กCoroutineContext.Key
๋ฅผ ๊ตฌํํ๋ Key๋ฅผ ๊ฐ๊ณ ์์
โ Key๋ฅผ ๋ช ์์ ์ผ๋ก ์ฌ์ฉํ์ง ์๊ณ ๊ตฌ์ฑ ์์ ์์ฒด๋ฅผ ํค๋ก ์ฌ์ฉ ๊ฐ๋ฅ
fun main() = runBlocking<Unit> {
val coroutineContext: CoroutineContext = CoroutineName("MyCoroutine") + Dispatcher.IO
val nameFromContext = coroutineContext[CoroutineName]
println(nameFromContext) // CoroutineName(MyCoroutine)
}
๊ตฌ์ฑ ์์์ key ํ๋กํผํฐ๋ฅผ ์ฌ์ฉ
- CoroutineContext์ ์ฃผ์ ๊ตฌ์ฑ ์์๋ค์ ๋ชจ๋ key ํ๋กํผํฐ๋ฅผ ๊ฐ์ง
- ์ด๋
companion object
๋ก ์ ์ธ๋ Key ์ ๋์ผํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด
- ์ด๋
fun main() = runBlocking<Unit> {
val coroutineName = CoroutineName("MyCoroutine")
val dispatcher = Dispatcher.IO
val coroutineContext = coroutineName + dispatcher
println(coroutineContext[coroutineName.key]) // CoroutineName("MyCoroutine")
println(coroutineContext[dispatcher.key]) // Dispatcher.IO
}
http://www.acornpub.co.kr/book/kotlin-coroutines
์ฝํ๋ฆฐ ์ฝ๋ฃจํด์ ์ ์
๋ง์ ๊ฐ๋ฐ์๋ค์ด ์ด๋ ต๊ฒ ๋๋ผ๋ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ๋ค์ํ ์๊ฐ์ ์๋ฃ์ ์ค๋ช ์ ํตํด ๋๊ตฌ๋ ์ฝ๊ฒ ์ดํดํ ์ ์๋๋ก ์ฐ์ธ ์ฑ ์ด๋ค.
www.acornpub.co.kr
๐ํ๊ธฐ๐
Spring Boot - Kotlin ์ฌ์ฉํ๋ ํ๊ฒฝ์์ ์ฝ๋ฃจํด์ ๋ํ ๋ ๊น์ ์ง์์ด ํ์ํด ์ฐพ๋ค ๋ฐ๊ฒฌํ ์ฑ .
ํ๊ตญ์ธ ์ ์ ๋ถ์ด์ด์ ๊ทธ๋ฐ์ง ์ ๋ฐ์ ์ผ๋ก ์ดํดํ๊ธฐ ๊ต์ฅํ ์ฝ๊ฒ ๋์ด์๊ณ ,
๊ธฐ์กด ์ฝํ๋ฆฐ ์ฑ ์์๋ ์ฝ๊ฒ ๋ค๋ค์ง์ง ์๋ ๋ ๋ฅํ ์ฝ๋ฃจํด ๊ธฐ๋ฅ๊น์ง ์ ์ ์์.
(์ง๊ธ๊น์ง ์ฝ์ ๊ธฐ์ ์ ์ค ์ค๋ฌด์์ ๊ฐ์ฅ ์ ์ฐ๊ณ ์๋ ์ฑ ...๊ฐ์ฅ ์ ์ ์ฝํ ์ฑ ...๐)
SpringBoot-Kotlin ํ๊ฒฝ์์ ์ฝ๋ฃจํด์ ๋ ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์ ๋ง ๊ฐ์ถ ํ์ ํ์ ๐๐ฅ๐๐ฅ๐๐ฅ๐๐ฅ๐๐ฅ