Created
December 6, 2020 10:33
-
-
Save ermolnik/43a0a1e003a2fb5cc7153e1130be7176 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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