Synchronized in instance context has effects if u apply 2 threads on the same runnable. Here u could create 2 threads like: A a = new A() Thread a1 = new Thread(a); Thread a2 = new Thread(a); In which case, for A would be enough just to implement Runnable as well. a1 and a2 can be also of type A in case A extends Thread, however.
On Jun 18, 10:58 am, ravi kiran <[email protected]> wrote: > I have a doubt regarding "Synchronized" in JAVA.... > > class A extends Thread > { > String name; > int no; > public synchronized void accept() > { BufferedReader br= new BufferedReader(new InputStreamReader(System.in); > > System.out.println("name"); > Thread.sleep(1000); > name=br.readLine(); > System.out.printl("no"); > Thread.sleep(1000); > no= Integer.parseInt(br.readLine());} > > public void display() > { > System.out.println("name:"+name); > Thread.sleep(1000); > System.out.println("no:"+no); > Thread.sleep(1000); > > } > > public void run() > { > accept(); > display();} > } > > class B > { > public static void main(String args[]) > > { > > A a1= new A(); > A a2= new A(); > a1.start(); > a2.start(); > > } > > Here accept() should work as a single Thread method and display should work > as a multi Thread method. But both the methods are working as multi Thread > methods. > > what is the problem? > -- > with regards ...kiran -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en
