Locks and synchronization

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() 
  println(s"CurrentThread: ${Thread.currentThread().getName}") 
  println(s"firstThread: ${firstThread.getName}") 

A few pointers for the preceding code:

  1. We simply created an object extending App to create the application entry point.
  2. 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. ...

Get Learning Scala Programming now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.