title: why I should "final" sharing variable in multi-threading program
body: <p>My question is why I should use final to decorate the variable, list? It is used by the instance of an anonymous inner class Without final, it won't compile.</p>
  
  <p>the code looks like this:</p>
  
  <pre><code>public class TwoThreadsOneListCurrModi
  {
    public static void main(String[] args)
    {    
       final List&lt;String&gt; list = Collections.synchronizedList(new ArrayList&lt;String&gt;());
  
      for (int i =0 ; i&lt;20;i++)
        list.add(String.valueOf(i));
      Thread t1 = new Thread(new Runnable(){
  
        @Override
        public void run()
        {
            synchronize(list) {
            System.out.println("size of list:" +list.size());
            }
        }
      });
  
      t1.start();  
    }
  }
  </code></pre>
  
  <p>But if I use normal class, it is fine.</p>
  
  <pre><code>public class TwoThreadsOneListCurrModi2
  
  {
    public static void main(String[] args)
    {    
       final List&lt;String&gt; list = Collections.synchronizedList(new ArrayList&lt;String&gt;());
      initialize list;
  
      Thread t1 = new WorkThread(list);
      Thread t2 = new WorkThread(list);    
      t1.start();  
      t2.start();
    }
  }
  class WorkThread extends Thread{
      List&lt;String&gt; list;
      public void run(){
         do sth with list and synchronize block on list
    }
    Work1(List&lt;String&gt; list)
    {    this.list = list;  }
  }
  </code></pre>