We talked about threads in previous sections—we'll first try to create a few ourselves before discussing them further. Let's write some code for that:
object ThreadFirstEncounter extends App { class FirstThread extends Thread { override def run(): Unit = println(s"FirstThread's run!") } val firstThread = new FirstThread() firstThread.start() println(s"CurrentThread: ${Thread.currentThread().getName}") println(s"firstThread: ${firstThread.getName}") }
A few pointers for the preceding code:
- We simply created an object extending App to create the application entry point.
- We created a class named FirstThread that extends Thread, which is nothing but the same java.lang.Thread we talked about in the previous section. ...