title: Scala treats "sharing" local variables in threading differently from Java? How does it work?
body: <p>My mental model of how local variables work is they are stored on the stack, and each thread has their own stack and stacks cannot be accessed between Threads.</p>
  
  <p>In Java, I'm actually not sure how I would modify a local variable in a spawned thread since the compiler complains I can't modify it in a lambda or in an inner class.</p>
  
  <p>However, in Scala I can do this:</p>
  
  <pre><code>implicit val ec = scala.concurrent.ExecutionContext.global
  
  var i = 5
  
  val f = Future {
    println((1, i))
    i = 6
    println((1, i))
  }
  val g = Future {
    println((2, i))
    println((2, i))
    println((2, i))
  }
  Await.result(f, 3.seconds)
  </code></pre>
  
  <p>and obtain a result of</p>
  
  <pre><code>(2,5)
  (1,5)
  (2,5)
  (2,6)
  (1,6)
  </code></pre>
  
  <p>How does thread #2 see the modification from thread #1?</p>