Build and run the following code that everyone runs first on the command line.
import kotlinx.coroutines.*
fun main() {
GlobalScope.launch { // launch a new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main thread continues while coroutine is delayed
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
first.kt
- Download kotlin compiler
The procedure is described on the official website (here)
- Download the kotlin coroutine jar file
When you execute the following command,
kotlinx-coroutines-core-1.3.5.jar
will be downloaded to the current directory.
mvn dependency:get -Dartifact=org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5 -Ddest=./
- Compile source code
When you execute the following command First.jar will be generated in the current directory.
kotlinc -cp kotlinx-coroutines-core-1.3.5.jar first.kt -include-runtime -d first.jar
- Run from command line
Execute the following command
java -cp ./first.jar:./kotlinx-coroutines-core-1.3.5.jar FirstKt
The reasons are the following three points
-When I run it on Playground, Hello
and World
are displayed at the same time, so I thought it was subtle.
--I wanted to build the kotlin source using the extension library from the command line.
--It was troublesome to launch Android Studio
Recommended Posts