#๊ธฐ๋ณธ์ ์๋ฐ
fun main(){
print("hello world") #mainํจ์๋ fun main(){}, ์ธ๋ฏธ์ฝ๋ก ์ ์์ด๋ ๋๊ณ ์์ด๋ ๋๊ณ
#prinln ๋ ์กด์ฌ
}
๊ธฐ๋ณธ ๋ณ์๋ var (๋ณ์๋ช ) = (๊ฐ)
๊ธฐ๋ณธ์ ์ผ๋ก ๋ณ์ํํ๋ ์์์ ์ถ๋ฆฌ
๋ณ์๋ช ์ ํ ์->var (๋ณ์๋ช ) : (Int, String, Double.....) = (๊ฐ)
์์๋->val (๋ณ์๋ช ) = (๊ฐ)
์์๋ ๊ฐ ๋ณ๊ฒฝ ๋ถ๊ฐ
์ ์ญ๋ณ์๋ ๋ฉ์ธ ํจ์ ๋ฐ์
๋ณ์ or์์ ์์ const ๋ถ์ผ ์ ๋ฉ์ธํจ์๋ณด๋ค ๋จผ์ ์ปดํ์ผ
var i = 10
var l = 20L
var name = "10"
i = name.toInt() #(๋ณ์๋ช
).to+(์ํ๋ ํ)() ํํ๋ก ์
๋ ฅ
๋ฒ ์ด์ค๊ฐjava
ใดuppercase,lowercase์ฌ์ฉ ๊ฐ๋ฅ
ใด๋ฐฐ์ด๋ ๋ณ์๋ช [] ์ฌ์ฉ ๊ฐ๋ฅ
ใดprintํ ๋ ๋ณ์๊ฐ ํ์ํ๋ฉด + ๋์
ใดKotlin.math.max(),min() ์ฌ์ฉ ๊ฐ๋ฅ
random ์ฌ์ฉํ ๋๋
val randeomNumber = Randem.nextInt(0,100) #0~99๊น์ง
์ฒ๋ผ ์ฌ์ฉ
val reader = Scanner(System.'in')
reader.nextInt() #์ซ์์
๋ ฅ
reader.next() #๋ฌธ์์
๋ ฅ
java์ ๋์ผ
var i = 5
when {
i>10 -> {
print("10๋ณด๋ค ํฌ๋ค")
}
i > 5 ->{
print("5๋ณด๋ค ํฌ๋ค")
}
}
๋ณ์์ if or when ์ ๋ ฅ ๊ฐ๋ฅ
var i = 5
var result = if (i>10) true else false
val items = listOf(1,2,3,4,5)
for (item in items){
print(item)
}
val items = listOf(1,2,3,4,5)
for (i in 0..(items.size -1)){
print(item)
}
items.forEach {item ->
print(item)
}
๋ณ๊ฒฝ๊ฐ๋ฅํ list๋ง๋ค๋๋
val items = mutableListOf(1,2,3,4,5)
items.add(6)
items.remove(3)
val items = arrayOf(1,2,3)
items[0] = 10
try{
} catch (e.message){
}
var name: String? = null #String? ๋ String์ ๋ค๋ฅด๋ค
null์ด ์๋ ๋ ์คํํ๋ ค๋ฉด
name?.let{
name2 = name
}
fun sum(a:Int, b:Int) : Int{
return a+b
}
๋๋
fun sum(a:Int, b:Int) = a+b
fun name(){
val jhon = Person("John", 20)
print(jhon.name, jhon.age)
}
class Person(val name: String, var age : Int,)
override๋ฅผ ํตํด ์ค์ or class ์์ data ๋ถ์ด๊ธฐ
data class Person()
class๊ฐ ์คํ๋ ๋๋ง๋ค ์คํ๋๊ฒ ํ๋ ค๋ฉด init ์ฌ์ฉ
class Person(){
init{
print("1")
}
}
init ๋ถ๋ถ์ private set ์์ฑ ์ ์ธ๋ถ์์ ๋ณ๊ฒฝ ๋ถ๊ฐ
class Person(){
var hobby = "์ถ๊ตฌ"
private set
get() = "์ทจ๋ฏธ": $field #์ฌ๊ธฐ๋ ๋ณ์๋ช
์ด ์๋ field ์ฌ์ฉ
init{
print("1")
}
}
fun main(){
}
class dog{
fun move(){
//
}
}
class cat{
fun move(){
//
}
}
์ ๋ฐ๊พธ๋ฉด
fun main(){
}
abstract ckass Animal{
open fun move(){ #์ฝํ๋ฆฐ์ ์์๋ฐ์๋ ์ฌ์ฉํ๋ ค๋ฉด open ํ์
print("์ด๋")
}
}
class dog : Animal(){
override fun move(){
println("๊ป์ถฉ")
}
}
class cat : Animal(){
override fun move(){
println("์ด๊ธ")
}
}
fun main(){
}
interface Drawable{
fun draw()
}
abstract ckass Animal{
open fun move(){ #์ฝํ๋ฆฐ์ ์์๋ฐ์๋ ์ฌ์ฉํ๋ ค๋ฉด open ํ์
print("์ด๋")
}
}
class dog : Animal(), Drawable{ #์๊ดํธ ์์
override fun move(){
println("๊ป์ถฉ")
}
}
class cat : Animal(){
override fun move(){
println("์ด๊ธ")
}
}
์ดํด ๋ ํ์
fun main(){
val dog : Animal = DOG()
val cat = Cat()
if(dog is Dog){
println("๋ฉ๋ฉ์ด")
}
}
interface Drawable{
fun draw()
}
abstract ckass Animal{
open fun move(){
print("์ด๋")
}
}
class dog : Animal(), Drawable{
override fun move(){
println("๊ป์ถฉ")
}
}
class cat : Animal(){
override fun move(){
println("์ด๊ธ")
}
}
fun main(){
val box = Box(10)
val box2 = Box("dfdfdf")
print(box.value)
}
class box<T>(var value: T){
}
์ดํด ๋ ํ์
fun main(){
myFunc(10){ #๋๋ค์?
println("ํจ์ ํธ์ถ")
}
}
fun myFunc(a:Int,callBack : () -> Unit){
println("ํจ์ ์์")
callBack()
println("ํจ์ ๋")
}
์ด๋์ ๋ ์ดํด ๋ ํ์?
์ฝ๋ฃจํด(Coroutine)์ ์ผ๋ฐ ๋ฃจํด๊ณผ๋ ๋ค๋ฅด๊ฒ ์ค๋จ๊ณผ ์ฌ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ฉฐ ์ค๋จํ ๊ณณ์์ ์ฌ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ค.
๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ฝ๋ฃจํด์ด ์์ฐจ์ ์ผ๋ก ํ ์ ์๋๋ก ๋์์ค๋ค.
suspend์ ๋ํ ์ดํด ๋ฐ ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ๋ํ ๊ณต๋ถ๊ฐ ๋ ํ์ํ ๋ฏ...
๋ฌผ๋ก ํ๋ฐ์ ๊ฐ์๋ก ์ด๋ ค์์ง์ง๋ง ์์ํ๊ธฐ์๋ ์ด๋ ต์ง ์๋ค
import kotlinx.coroutines.*
fun main(){
GlobalScope.launch{this: CoroutinesScope //launch:์ฝ๋ฃจํด ๋น๋, GlobalScope:๋ฉ์ธ ์ฝ๋๊ฐ ์๋๋ ํ ์คํ.
delay(timeMillis: 1000L)
println("World!")
}
println("Hello,")
Thread.sleep(2000L)
}
ใดํ๋ฒWorld!๋ฅผ ์คํ ํ ๊ณ์ ์คํํ๋ค๋๋ฐ ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋ ์ด์ World!๊ฐ 2๋ฒ ์ด์ ์ธจ๋ ฅ๋์ง ์๋๋ค๊ณ ํจ...์ด์จฐ์..?
ใด๋ค์ ์๊ฐํด๋ณด๋ ๋ฉ์ธ ์ฝ๋์ ์ฝ๋ฃจํด์ ๋์์ ์คํ์ํค๋๋ฐ ํ๋ก๊ทธ๋จ ์ข ๋ฃ ์กฐ๊ฑด์ ๋ฉ์ธ ํ๋ก๊ทธ๋ญ ์ข ๋ฃ์ด๋ ๊ทธ๋ฐ๋ฏ?
import kotlinx.coroutines.*
fun main(){
GlobalScope.launch{this: CoroutinesScope
delay(timeMillis: 1000L)
println("World!")
}
println("Hello,")
runBlocking{this:CoruotineScope //์ฝ๋ฃจํด ์คํ์ฉ runBlocking
delay(timeMillis: 2000L)
}
}
import kotlinx.coroutines.*
fun main() =
runBlocking{this: CoroutineScope
GlobalScope.launch{this: CoroutinesScope //launch:์ฝ๋ฃจํด ๋น๋, GlobalScope:๋ฉ์ธ ์ฝ๋๊ฐ ์๋๋ ํ ์คํ.
delay(timeMillis: 1000L)
println("World!")
}
println("Hello,")
delay(timeMillis: 2000L)
}
import kotlinx.coroutines.*
fun main() =
runBlocking{this: CoroutineScope
val job = GlobalScope.launch{this: CoroutinesScope
delay(timeMillis: 3000L)
println("World!")
}
println("Hello,")
job.join() //์๋ง ํจ์ ์คํ์ํค๋ ๊ฑฐ๋ก ๋ณด์
}
import kotlinx.coroutines.*
fun main() =
runBlocking{this: CoroutineScope
this.launch{this: CoroutinesScope //launch:์ฝ๋ฃจํด ๋น๋, GlobalScope:๋ฉ์ธ ์ฝ๋๊ฐ ์๋๋ ํ ์คํ.
delay(timeMillis: 3000L)
println("World!")
}
launch{this: CoroutinesScope //launch:์ฝ๋ฃจํด ๋น๋, GlobalScope:๋ฉ์ธ ์ฝ๋๊ฐ ์๋๋ ํ ์คํ.
delay(timeMillis: 3000L)
println("World!")
}
println("Hello,")
}
๊ฒฐ๋ก ์ ์ผ๋ก ์คํ๊น์ง ๊ธฐ๋ค๋ ค์ฃผ๋ ๊ฒ์ด ์คํธ๋ญ์ณ๋ ์ปจํฌ๋ฌ์์ด๊ณ ์๋นํ ์์ฃผ ๋์จ๋ค.
import kotlinx.coroutines.*
fun main() =
runBlocking{this: CoroutineScope
launch{this: CoroutinesScope
myWorld()
}
println("Hello,")
}
suspend fun myWorld(){
delay(timeMillis: 1000L)
println("world!")
}
ใด์์คํ๋ ํ์ (์:delay)๋ ์์คํ๋ ๋ฐ ์ฝ๋ฃจํด์์๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค
fun main() = runBlocking{
repeat(100_000){
launch{
delay(1000L)
print(".")
}
}
}
ใด์ฝ๋ฃจํด์ด ๊ฐ๋ณ๋ค ์ฆ๋ช ์ฉ ์ฝ๋
import kotlinx.coroutines.*
fun main() =
runBlocking{this: CoroutineScope
GlobalScope.launch {this: CoroutineScope
repeat(times:1000) {i->
println("I'm sleeping $i ...")
delay(timeMillis: 500L)
}
}
delay(timeMillis: 1300L)
}
ใด๋ฉ์ธํ๋ก์ธ์ค๊ฐ ๋๋ ์ ์ฝ๋ฃจํด๋ ๋๋๋ค๋ ์ฝ๋
import kotlinx.coroutines.*
fun main() =
runBlocking{this: CoroutineScope
launch {this: CoroutineScope
repeat(times:5) {i->
println("Coroutine A, $i ...")
delay(timeMillis:10L)
}
}
launch {this: CoroutineScope
repeat(times:5) {i->
println("Coroutine B, $i ...")
delay(timeMillis:10L)
}
}
println("Coroutine Outer")
}
ใด์ฝ๋ฃจํด์ ์ค๋จ ๋ฐ ์ฌ๊ฐ์ ์์ ์ฝ๋
๋ฉ์ธ ์ฝ๋์ ์ฝ๋ฃจํด์ ๋์์ ์คํ์ด ๊ฐ๋ฅํ์ง๋ง ์ฝ๋ฃจํด๋ผ๋ฆฌ๋ ์ฑ๊ธ ์ค๋ ๋์์ ๋์ํ์ฌ์
๋ด๋ถ ์์ ์ ์์ฐจ์ ์ผ๋ก ์คํ๋๋ค.
๋ณ๋ ฌ๋ก ๋์ํ๋ ค๋ฉด ๋์์ฑ์ ์ ์ดํด์ผ ํ๋ค.(await,async,delay๋ฑ ์ฌ์ฉ)
import kotlinx.coroutines.*
fun main() = runBlocking { this: CoroitineScope
val job = launch { this: CoroutinesScope #์บ์ฌ ๊ฐ๋ฅ ๊ฐ์ฒด
repeat(time:1000) {i ->
prinln(msg:"job: I'm sleeping $i ...")
delay(timeMillis: 500L)
}
}
delay(timeMillis: 1300L)
println(msg: "main: I'm tried of waiting!")
job.cancle()
job.join()
println(msg: "main: Now I can quit")
}
์คํ๊ณผ ๋์์ job์ด ์คํ->1.3์ด ํ์ cancle์ด ์คํ๋๊ณ ์ ์งํจ->(๊ทธ๋ผ join์ ์ด์งธ์ ์กด์ฌํ๋๊ฐ?)????
import kotlinx.coroutines.*
fun main() = runBlocking {
val startTime = currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
var i = 0
while (i < 5) { // computation loop, just wastes CPU
// print a message twice a second
//yield()
if (currentTimeMillis() >= nextPrintTime) {
println("job: I'm sleeping ${i++} ...")
nextPrintTime += 500L
}
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
}
cancleandjoin์ด ์ ์๋๋์ง ์๋ ์ด์ ๋ delay๊ฐ์ ์์คํ๋ ํ์ ์ด ์์ด์
yield()๊ฐ ์์ผ๋ฉด delay์์ด๋ ๊ฐ๋ฅ
import kotlinx.coroutines.*
fun main() = runBlocking {
val startTime = currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
var i = 0
while (isActive) { // cancellable computation loop
// print a message twice a second
if (currentTimeMillis() >= nextPrintTime) {
println("job: I'm sleeping ${i++} ...")
nextPrintTime += 500L
}
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
}
isActive๋ ์ฝ๋ฃจํด์ด ๋์๊ฐ ์ ์๋ ์ํ์ธ์ง ์๋์ง๋ฅผ ํ๋ณํ๋ ๊ฒ์ธ๋ฐ cancle๋ก ์ธํด ๋ถํ๋์ด์ ์ฝ๋ ์ ์ง
import kotlinx.coroutines.*
fun main() = runBlocking {
val job = launch {
try {
repeat(1000) { i ->
println("job: I'm sleeping $i ...")
delay(500L)
}
} finally {
println("job: I'm running finally")
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
}
cancle์ด ์ต์ ์ ์ ๋ฐ์์ํค๊ณ ๊ทธ๊ฒ์ผ๋ก ์ธํด try๊ฐ ์๋ finally๊ฐ ์คํ๋๋ค
import kotlinx.coroutines.*
fun main() = runBlocking {
val job = launch {
try {
repeat(1000) { i ->
println("job: I'm sleeping $i ...")
delay(500L)
}
} finally {
withContext(NonCancellable) {
println("job: I'm running finally")
delay(1000L)
println("job: And I've just delayed for 1 sec because I'm non-cancellable")
}
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
}
rareํ ์ผ์ด์ค, ์ ์ง๋ ์ฝ๋ฃจํด ์์์ ์ฝ๋ฃจํด์ด ์คํ๋๋ ๊ฒฝ์ฐ
import kotlinx.coroutines.*
fun main() = runBlocking {
withTimeout(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
}
0.5์ด์ฉ 1000๋ฒ ๋ฐ๋ณตํ๋ ์ฝ๋์ธ๋ฐ withTimeout์ ํตํด ์ ์ง์ํจ๋ค.(ํ๋ ๋ฉ์ธ ์ฝ๋์ฌ์ ๊ทธ๋ฐ์ง ์ต์ ์ ์ด ๋ฐ์ํ๋ค๊ณ ํ๋ค.)
import kotlinx.coroutines.*
fun main() = runBlocking {
val result = withTimeoutOrNull(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
"Done" // will get cancelled before it produces this result
}
println("Result is $result")
}
์ด๋ฒ์๋ ์๊ฐ์ด ์ง๋ ์ null๊ฐ์ผ๋ก ๋ฐ๋๊ณ ๊ฐ์ด "Done"์ผ๋ก ๋ฐ๋๋ ์ฝ๋์ด๋ค.
if๋ฌธ๋ ์๋๋ฐ ์ฌ์ค ์ "Done"๊ฐ์ด ๋์ค๋์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค.
ใด์ฐพ์๋ณด๋ whitTimeOrNull์ด ๋ง์ง๋ง์ผ๋ก ๋์จ ํํ์์ ๊ฐ์ ๋ฐํํ๋ค๊ณ ํด์ "Done"์ด ์ถ๋ ฅ๋๋ ๋ฏ ํ๋ค.(if๋ฌธ ๋น์ซํ ๊ฒ ๊ฐ๊ธฐ๋..?)
import kotlinx.coroutines.*
import kotlin.system.*
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = doSomethingUsefulOne()
val two = doSomethingUsefulTwo()
println("The answer is ${one + two}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L) // pretend we are doing something useful here, too
return 29
}
์์ฐจ์ ์ผ๋ก ๋์๊ฐ๋ ์ฝ๋
import kotlinx.coroutines.*
import kotlin.system.*
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L) // pretend we are doing something useful here, too
return 29
}
์์ฐจ์ ์ผ๋ก ->๋น๋๊ธฐ์ ์ผ๋ก ์คํ(์คํ์๊ฐ ์ค์ด๊ธฐ),async์คํ์ ์ฝ๋ ์คํ ์์๋ง ํ๊ณ ๋ฐ๋ก ๋ค์์ค๋ก ๋์ด๊ฐ,await->async ์๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๊ธฐ
import kotlinx.coroutines.*
import kotlin.system.*
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
// some computation
one.start() // start the first one
two.start() // start the second one
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L) // pretend we are doing something useful here, too
return 29
}
๋ผ์ธ์ start์ LAZY๋ฅผ ๊ฑธ์์ ๋ start๋ฅผ ๊ฑธ์ด์ผ ์๋ ์์๋๋๋ก ์ฝ๋ฉ,๋ง์ฝ start๋ฅผ ํ์ง ์์ ์ await๋ฅผ ๋ง๋์ผ ์คํ์ด ๋์ 2์ด๊ฐ ๊ฑธ๋ฆผ
import kotlinx.coroutines.*
import kotlin.system.*
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
// some computation
one.start() // start the first one
two.start() // start the second one
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L) // pretend we are doing something useful here, too
return 29
}
์ ์์๋ ํ์ง ๋ง๋ผ๋ ์์์ด๋ค. ์ ์ฝ๋๋ฅผ ์คํํ ์ ํฐ์ผ์ด ๋ฒ์ด์ง(์ธ๋ถํจ์์ฌ์ ์ต์ ์ ์ด ๋ฐ์ํ์ฌ๋ ์ค์ง๊ฐ ๋์ง ์๋๋ค)(๋ ์๊ฐ ํ์)
import kotlinx.coroutines.*
import kotlin.system.*
fun main() = runBlocking<Unit> {
val time = measureTimeMillis {
println("The answer is ${concurrentSum()}")
}
println("Completed in $time ms")
}
suspend fun concurrentSum(): Int = coroutineScope {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
one.await() + two.await()
}
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
}
suspend fun doSomethingUsefulTwo(): Int {
delay(1000L) // pretend we are doing something useful here, too
return 29
}
์ด ๊ฒฝ์ฐ๋ scope์์ ์ฐ๋ค ๋ณด๋ ์ต์ ์ ์ด ๋ฐ์ํ๋ฉด ์บ์ฌ๋๋ค,์คํธ๋ญ์ณ๋ ์ปจํฌ๋ฌ์ฌ ํํ๋ก ๋ง๋ค์ด์ ํ๋๊ฒ ์ข๋ค
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
try {
failedConcurrentSum()
} catch(e: ArithmeticException) {
println("Computation failed with ArithmeticException")
}
}
suspend fun failedConcurrentSum(): Int = coroutineScope {
val one = async<Int> {
try {
delay(Long.MAX_VALUE) // Emulates very long computation
42
} finally {
println("First child was cancelled")
}
}
val two = async<Int> {
println("Second child throws an exception")
throw ArithmeticException()
}
one.await() + two.await()
}
์คํ-one,two์คํ-two์์ ์ถ๋ ฅ ํ ์ต์ ์ ๋ฐ์-์ ํ-one์์ finally๋ก ์ฐ๊ฒฐ-์ถ๋ ฅ-์ ํ๋ก ์ธํด ๋ฉ์ธ์์ catch๋ก ์ด๋-์ถ๋ ฅ
์์ฐจ์ ์ผ๋ก ์์ฑํ์์๋ ์ด๋ป๊ฒ ๋น๋๊ธฐ์ ์ผ๋ก ๋์๊ฐ๋์ง
ใดํจ์์ ๋์ ํ๋ผ๋ฏธํฐ๊ฐ ํ๋ ์ถ๊ฐ๋์ด ์ปจํฐ๋ด์์ด์ ์ด๋ผ๋ ๊ฐ์ฒด๋ฅผ ๋๊ฒจ์ฃผ๋ ํํ๋ก ๋ณ๊ฒฝ
ใด์ฝ๋์์ ์คํํ ๋ LABEL์ ๋ฃ์ด์ ์ค๋จ ๋ฐ ์ฌ์ ๊ธฐ์ค์ ๋ง๋ฌ
ใดswtich case๋ฌธ ๊ฐ์ ๋๋
ใด์ปจํฐ๋ด์์ด์ ..?
ใด์ฝ๋ฐฑ(?)๊ฐ์ ๋๋
์ฝ๋ ์ด ํ์ Tools - Kotlin - Show Kotlin Bytecode - ๋์ปดํ์ผ
์ฝํ๋ฆฐ ์ฝ๋๊ฐ ๋ถํด? ๋์ด ๋ณด์ฌ์ง
switch case๋ฌธ์ด ๋ณด์ธ๋ค.
GlobalScope.launch {
val userData = fetchUserData()
var userCache = cacheUserData(userData)
updateTextView(userCache)
}
ใดwhen๋ฌธ ์์์ ๊ธฐ์ค์ด ๋๋ ๋ณ์์ ๊ฐ์ ๋๋ ค๊ฐ๋ฉด์ ์์ฐจ์ ์คํ์ฒ๋ผ ๋ณด์
ใด๋ํ when๋ฌธ์ด๊ธฐ ๋๋ฌธ์ ์ค๋จ ๋ฐ ์ฌ์์ด ๊ฐ๋ฅํ๋ค
๊ฒฐ๋ก :์ฝ๋ฐฑ์ ๋ด๊ฐ ํ๋๊ฒ์ด ์๋ ์ฝํ๋ฆฐ ์ปดํ์ผ๋ฌ๊ฐ ํ๋ ๊ฒ
(์ด๋์ ๋ ์ดํดํ๋ ค๋ฉด ์ฌ๋ฌ๋ฒ ๋ด์ผํ ๋ฏ ํ๋ค)
์ฝ๋ฃจํด์ด ์ด๋ค ์ค๋ ๋์์ ์คํ์ํฌ์ง ๊ฒฐ์ ํด์ฃผ๋ ์์
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
}
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
}
launch(Dispatchers.Default) { // will get dispatched to DefaultDispatcher
println("Default : I'm working in thread ${Thread.currentThread().name}")
}
launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
}
}
์ฝ๋ฃจํด 4๊ฐ ์คํ์์:2-3-4-1
2๋ฒ:main ์ค๋ ๋์์ ์คํ
3๋ฒ:worker-1์์ ์คํ
4๋ฒ:๋น์ฉ์ด ๋์.ํ๋ฒ ์ฌ์ฉ๋ง๋ค ์ค๋ ๋๋ฅผ ๋ง๋ฌ,์ค์ฌ์ฉ์์๋ newSingleThreadContext("MyOwnThread").use๋ก ์ฐ๋๊ฒ ์ข์
1๋ฒ:runblocking์์ ์ต์ ๋ ํ๋ผ๋ฏธํฐ ์์ด ์คํ๋จ->๊ทธ๋ด์ ์์ ์ ํธ์ถํ๋ ๊ณณ(๋ฉ์ธ)์์ ์คํ
๋์คํ์ฒ:์ด๋์ ์ฝ๋ฃจํด์ผ ์คํ๋ ์ง ์ ํ๋ฉฐ ์ฝ๋ฃจํด ์ฝํ ์คํธ์ ์ ์ฅ๋๋ค
import kotlinx.coroutines.*
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
fun main() = runBlocking<Unit> {
val a = async {
log("I'm computing a piece of the answer")
6
}
val b = async {
log("I'm computing another piece of the answer")
7
}
log("The answer is ${a.await() * b.await()}")
}
์ฝํ๋ฆฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ฝ๋ฃจํด์ ๋๋ฒ๊น ํ๊ธฐ ์ํ ๋๊ตฌ๊ฐ ์๋ค.
log๋ผ๋ ํจ์๋ ์ง๊ธ ์ด๋์ ์คํ๋๊ณ ์๋์ง ์๋ ค์ค๋ค.
๋ง์ฝ ์ค๋ ์ฝ๋ฃจํด์์ ์คํ๋๊ณ ์๋์ง ๋ณผ๋ ค๋ฉด jvm option์์ ์ค์ ์ ์ถ๊ฐํ๋ค.
import kotlinx.coroutines.*
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
fun main() {
newSingleThreadContext("Ctx1").use { ctx1 ->
newSingleThreadContext("Ctx2").use { ctx2 ->
runBlocking(ctx1) {
log("Started in ctx1")
withContext(ctx2) {
log("Working in ctx2")
}
log("Back to ctx1")
}
}
}
}
withcontext๋ฅผ ํตํด ์ค๋ ๋๊ฐ์ ์ ํํ ์ ์๋ค.
use๋ฅผ ํตํด ์ค๋ ๋๋ฅผ ํด๋ก์ฆ ํด์คฌ๋ค.(?)
ใด...(๊ณต๋ถ ํ์)
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
println("My job is ${coroutineContext[Job]}")
}
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
// launch a coroutine to process some kind of incoming request
val request = launch {
// it spawns two other jobs
launch(Job()) {
println("job1: I run in my own Job and execute independently!")
delay(1000)
println("job1: I am not affected by cancellation of the request")
}
// and the other inherits the parent context
launch {
delay(100)
println("job2: I am a child of the request coroutine")
delay(1000)
println("job2: I will not execute this line if my parent request is cancelled")
}
}
delay(500)
request.cancel() // cancel processing of the request
println("main: Who has survived request cancellation?")
delay(1000) // delay the main thread for a second to see what happens
}
์ฝ๋ฃจํด์ด ์คํ๋ ์ ์ฝ๋ฃจํด์ ๋ถ๋ชจ์ ์์ ์ฝ๋ฃจํด์ด ๋๋ค.
996์ค์ ์ฝ๋ฃจํด์ ์์ launch์ ์์์ด ๋๋ ๊ฒ
๋์ globalscope์ ๊ฒฝ์ฐ๋ ์์ธ์ด๋ค.
์ ์ฝ๋์ ๊ฒฝ์ฐ๋ globalscope๋ ์์ธ์ ๊ฒฝ์ฐ๋ผ์ ๋๊น์ง ์คํ์ด ๋์์ง๋ง ์ผ๋ฐ ์์ ์ฝ๋ฃจํด์ ๊ฒฝ์ฐ์๋
๋ถ๋ชจ๊ฐ cancle๋์ด์ ์์๋ cancle๋์๋ค.
๋ถ๋ชจ ์ฝ๋ฃจํด์ ๋ชจ๋ ์์ ์ฝ๋ฃจํด๋ค์ด ์คํ๋ ๋๊น์ง ๊ธฐ๋ค๋ ค์ค(์ง์ ํธ๋ํน(?) ํ์์์)
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
// launch a coroutine to process some kind of incoming request
val request = launch {
repeat(3) { i -> // launch a few children jobs
launch {
delay((i + 1) * 200L) // variable delay 200ms, 400ms, 600ms
println("Coroutine $i is done")
}
}
println("request: I'm done and I don't explicitly join my children that are still active")
}
request.join() // wait for completion of the request, including all its children
println("Now processing of the request is complete")
}
๋ถ๋ชจ ์ฝ๋๊ฐ ๋ค ์คํ๋์์์ ์๋ฆฐ ํ์๋ ์์ ์ฝ๋ฃจํด์ด ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ์ ์๋ ค์ฃผ๋ ์ฝ๋
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
launch(Dispatchers.Default + CoroutineName("test")) {
println("I'm working in thread ${Thread.currentThread().name}")
}
}
์ฌ๋ฌ๊ฐ์ ์ฝ๋ฃจํด ์๋ฆฌ๋จผํธ(?)๋ฅผ ๋ฃ๊ณ ์ถ์๋๋ + ์ฌ์ฉํ๊ธฐ,(+๋ ์์ฒด์ ์ผ๋ก ๋ํ๋ ๊ธฐ๋ฅ์ด ์์)
import kotlinx.coroutines.*
class Activity {
private val mainScope = CoroutineScope(Dispatchers.Default) // use Default for test purposes
fun destroy() {
mainScope.cancel()
}
fun doSomething() {
// launch ten coroutines for a demo, each working for a different time
repeat(10) { i ->
mainScope.launch {
delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc
println("Coroutine $i is done")
}
}
}
} // class Activity ends
fun main() = runBlocking<Unit> {
val activity = Activity()
activity.doSomething() // run test function
println("Launched coroutines")
delay(500L) // delay for half a second
println("Destroying activity!")
activity.destroy() // cancels all coroutines
delay(1000) // visually confirm that they don't work
}
ใด์คํ(dosomthing)์ ํ๋ค๊ฐ ๋ฉ์ถ๋ฉด(destroy) ์คํ ๋ฉ์ถ๊ธฐ(cancle)
์๋๋ก์ด๋ ์ฑ์ ์ฐ๋ค๊ฐ ๋๋ฉด์ ์์ ์ ๋ฉ์ถ์ด์ผ ํ๋ค. ๊ทธ๊ฒ์ ์ํด ์ฝ๋ฃจํด ์ค์ฝํ๊ฐ ์๋ค.