1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /**
  7.  *
  8.  * @author martijn
  9.  */
  10. public class Counter
  11. {
  12.  
  13.     public int i;
  14.  
  15.     public static void main(String[] args)
  16.     {
  17.         sephyDemoNotWorking();
  18.         try {
  19.             Thread.sleep(2000);
  20.         } catch (Exception e) {
  21.         }
  22.         ;
  23.  
  24.         sephyDemoWorking();
  25.     }
  26.  
  27.     private static void sephyDemoNotWorking()
  28.     {
  29.         final Counter c = new Counter();
  30.         c.i = 1;
  31.  
  32.         do {
  33.             Thread t = new Thread(new Runnable()
  34.             {
  35.  
  36.                 @Override
  37.                 public void run()
  38.                 {
  39.                     try {
  40.                         Thread.sleep(100); // Thread.sleep which simulates the tv.post
  41.                     } catch (Exception e) {
  42.                     }
  43.                     System.out.println(c.i);
  44.                 }
  45.             });
  46.             t.start();
  47.             c.i++;
  48.             try {
  49.                 Thread.sleep(10);
  50.             } catch (Exception e) {
  51.             }
  52.         } while (c.i < 16);
  53.     }
  54.  
  55.     private static void sephyDemoWorking()
  56.     {
  57.         int i = 1;
  58.  
  59.         do {
  60.             final int localCopy = i;
  61.             Thread t = new Thread(new Runnable()
  62.             {
  63.  
  64.                 @Override
  65.                 public void run()
  66.                 {
  67.                     try {
  68.                         Thread.sleep(100); // Thread.sleep which simulates the tv.post
  69.                     } catch (Exception e) {
  70.                     }
  71.                     System.out.println(localCopy);
  72.                 }
  73.             });
  74.             t.start();
  75.             i++;
  76.             try {
  77.                 Thread.sleep(10);
  78.             } catch (Exception e) {
  79.             }
  80.         } while (i < 16);
  81.     }
  82. }