title: Multi-threading program to print numbers from 1 to 50?
body: <p>im trying to write a program in which two threads are created and the output should be like 1st thread prints 1 and the next thread prints 2 ,1st thread again prints 3 and so on. im a beginner so pls help me clearly. i thought thread share the same memory so they will share the i variable and print accordingly. but in output i get like thread1: 1, thread2 : 1, thread1: 2, thread2 : 2 nd so on. pls help. here is my code</p>
  
  <pre><code>class me extends Thread
  {
  public int name,i;
  public void run()
  {
      for(i=1;i&lt;=50;i++)
      {
          System.out.println("Thread" + name + " : " + i);
          try
          {
              sleep(1000);
          }
          catch(Exception e)
          {
              System.out.println("some problem");
          }
      }
  }
  }
  public class he
  {
  public static void main(String[] args)
  {
      me a=new me();
      me b=new me();
      a.name=1;
      b.name=2;
      a.start();
      b.start();
  }
  }
  </code></pre>