2 Replies Latest reply: Dec 11, 2007 6:44 AM by 807603 RSS

    Why my synchronized method being called by multiple Threads simultaneaously

    807603
      In this code below, I have made the myMethod() synchronized, expecting that whichever thread executes it would not be interrupted by some other thread. But well, it is not happening so... What should I do in this to achieve synchronization?

      class myThread extends Thread
        {
           
           String name;
       
           myThread(String n)
             { 
                name=n;
             }
              
           public void run()
              {
                 myMethod();
              }
       
           public synchronized void myMethod()
              {
                   for(int i=0;i<10;i++) 
                 System.out.println(name+":  "+i);
                 System.out.println("============");
              }
                
        }
       
      class ThreadsExec
       {
          
          public static void main(String[] args)
             {
                myThread a = new myThread("Thread A");
                myThread b = new myThread("Thread B");
                a.start();   
                b.start();
            
             }
       }
      Edited by: Rambhupal on Dec 10, 2007 10:16 PM