title: How to make object (a mutable stack) thread-safe?
body: <p>How to make Scala object thread-safe.</p>
  
  <pre><code>class Stack {
      case class Node(value: Int, var next: Node)
  
      private var head: Node = null
      private var sz = 0
  
      def push(newValue: Int) {
          head = Node(newValue, head)
          sz += 1
      }
  
      def pop() = {
          val oldNode = head
          head = oldNode.next
          oldNode.next = null
          sz -= 1
      }
  
      def size = sz    //I am accessing sz from two threads
  }
  </code></pre>
  
  <p>This class is clearly not threadsafe. I want to make it threadsafe.</p>
  
  <p>Thanks in Advance,</p>
  
  <p>HP</p>