Kotlin Thread 생성 및 실행
코틀린에서 스레드를 구현하는 방법은 다음의 4가지가 있다.
Thread 클래스 상속으로 구현
클래스를 상속 받아 구현한 예제는 아래와 같다.
class ThreadExtends : Thread(){
    override fun run() {
        println("Hello! This is extends ${currentThread()}")
    }
}
fun main() {
    val thread = ThreadExtends()
    thread.start()
}
Output:
Hello! This is extends Thread[Thread-0,5,main]
Runnable 인터페이스 상속으로 구현
Runnable 인터페이스 구현한 예제는 아래와 같다.
class ThreadRunnable : Runnable {
    override fun run() {
        println("Hello! This is runnable ${hashCode()}")
    }
}
fun main() {
    val thread = Thread(ThreadRunnable())
    thread.start()
}
Output:
Hello! This is runnable 109369515
익명 객체로 구현
익명 객체로 구현한 예제는 아래와 같다.
fun main() {
    val thread = object : Thread() {
        override fun run() {
            println("Hello! This is object Thread ${currentThread()}")
        }
    }
    thread.start()
}
Output:
Hello! This is object Thread Thread[Thread-0,5,main]
람다식으로 구현
람다식으로 구현한 예제는 아래와 같다.
fun main() {
    var thread = Thread {
        println("Hello! This is lambda thread ${Thread.currentThread()}")
    }
    thread.start()
}
Output:
Hello! This is lambda thread Thread[Thread-0,5,main]
아래와 같이 람다식으로 객체를 만들어 행위를 추가한 함수로 구현할 수도 있다.
fun startThread(): Thread {
    val thread = Thread {
        println("Hello! This is start lambda thread ${Thread.currentThread()}")
    }
    thread.start()
    return thread;
}
fun main() {
    startThread()
}
Output:
Hello! This is start lambda thread Thread[Thread-0,5,main]