val runnable: Runnable = Runnable {
println("Thread is running")
}
val t = Thread(runnable)
println(t.state) // NEW
t.start()
println(t.state) // RUNNABLE
class BlockRunnable: Runnable {
override fun run() {
commonResource();
TODO("Not yet implemented")
}
@Synchronized
fun commonResource() {
while(true) { }
}
}
fun main(args: Array<String>) {
val thread1 = Thread(BlockRunnable())
val thread2 = Thread(BlockRunnable())
thread1.start();
thread2.start();
Thread.sleep(1000);
System.out.println(thread2.getState()); // BLOCKED
}
fun inner() : Runnable= Runnable { try {
Thread.sleep(1000)
} catch (e: InterruptedException) {
Thread.currentThread().interrupt()
e.printStackTrace()
}
println(thread.state) // WAITING
}
fun waiting() = Runnable {
val inner = Thread(inner())
inner.start()
try {
inner.join()
} catch (e: InterruptedException) {
Thread.currentThread().interrupt()
e.printStackTrace()
}
}
val thread:Thread = Thread(waiting())
fun main() {
thread.start();
}
fun timeWaiting() = Runnable {
Thread.sleep(5000)
}
val thread:Thread = Thread(timeWaiting())
fun main() {
thread.start();
Thread.sleep(1000)
println(thread.state) // TIME WAIT
}
fun timeWaiting() = Runnable {
println("TERMINATE")
}
val thread:Thread = Thread(timeWaiting())
fun main() {
thread.start();
Thread.sleep(1000)
println(thread.state)
}