i am not understanding the output of the above program
as i have declared fname() method as synchronized so accrding to me output will be
0,1,2,3,4 for thread1
and 0,1,2,3,4 for thread2
or else i am doing mistake in my code can you explain that how we can achieve the desired output for different threads with the help of suitable example
Yes, you have declared the method to be synchronized, but you have two threads that are using two different MyThread objects.
The first one will synchronize on the first MyThread object, and the second one on the second MyThread object. Since these are separate objects, the two threads will not wait for each other. Synchronizing will make threads wait only if you let both threads synchronize on a shared object. Like this: