1 Reply Latest reply: Oct 2, 2007 4:43 AM by 807605 RSS

    Thread Synchronization Problem

    807605
      Hello:
      I have faced a problem on synchronization.
      i am getting output like
      Hello From: one
      Bye From: one
      Hello From: two
      Bye From: two
      so on
      if i use public void resource()

      also i am getting same output, if i use

      public synchronized void resource()

      what is the reason? and what is the solution of the given program?

      class Test{

      public synchronized void resource(){

      try{

      Thread.sleep(6000);

      System.out.println("Hello From :"+ Thread.currentThread().getName());


      }

      catch(Exception e){

      System.out.println(e);

      }


      System.out.println("Bye From :"+ Thread.currentThread().getName());

      }

      }


      public class Test1 implements Runnable{
      Test tt= new Test();

      Thread t;

      Test1(String tname){

      Thread t=new Thread(this,tname);

      t.start();


      }

      public void run(){

      tt.resource();

      }

      public static void main(String args[]){

      new Test1("one");
      new Test1("two");
      new Test1("three");

      }

      }