title: What object do I synchronize on in Scala?
body: <p>In C# it's pretty straightforward:</p>
  
  <pre><code>class Class1{
    private static readonly object locker = new object();
    void Method1(){
      lock(locker) { .... }
    }
  }
  </code></pre>
  
  <p>And I definitely should not make a synchronization on <code>this</code> because it might lead to a deadlock. Likewise, in Scala I saw examples and couldn't get the idea of what is the basic principle of synchronization and object (field) I should use to make a synchronization:</p>
  
  <pre><code>#1
  def add(user: User) {
    // tokenizeName was measured to be the most expensive operation.
    val tokens = tokenizeName(user.name)
  
    tokens.foreach { term =&gt;
      userMap.synchronized {
        add(term, user)
      }
    }
  }
  
  #2
  class Person(var name: String) {
    def set(changedName: String) {
      this.synchronized {
        name = changedName
      }
    }
  }
  
  #3 and so on...
  </code></pre>
  
  <p>Would you mind making it clear for me?</p>