Skip to content

Instantly share code, notes, and snippets.

@ermolnik
Created December 6, 2020 10:33
Show Gist options
  • Save ermolnik/43a0a1e003a2fb5cc7153e1130be7176 to your computer and use it in GitHub Desktop.
Save ermolnik/43a0a1e003a2fb5cc7153e1130be7176 to your computer and use it in GitHub Desktop.
class Printer {
fun printPages() {
for (i in 1..5) {
println("Printing page --- $i")
}
}
}
class SampleThread(private val threadName: String, var printer: Printer) : Thread() {
private var thread: Thread = Thread(this, threadName)
override fun run() {
synchronized(printer) { printer.printPages() }
println("Thread $threadName exiting.")
}
override fun start() {
println("Starting $threadName")
thread.start()
}
}
object EntryPoint {
@JvmStatic
fun main(args: Array<String>) {
val printDemo = Printer()
val threadOne = SampleThread("Thread - 1 ", printDemo)
val threadTwo = SampleThread("Thread - 2 ", printDemo)
threadOne.start()
threadTwo.start()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment