For this I got the output as :
My Doubt is : when the first Thread 's' has been started in gets the lock on the getTotal synchronized method. Then no other Thread can enter that method. but why the output is showing a mix of both Threads??
But if I run the same codeby implementing Runnable then Then Thread 1 is running after the Thread 0 has been completed.
Output is mixed because you are executing two threads which are created from two different objects so synchronization can not be achieved.
Post your code with Runnable interface, if code is same like above (creating two objects) it also should not give expected output.
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Joined: Oct 13, 2008
please use code tags
SCJP5 and SCWCD1.5
Think Twice Act Wise...
Joined: Apr 23, 2007
Yes you are right. I made a mistake by passing same runnable target to both threads.
Here t and t1 are having same Runnable target. If so then we will get Thread1 runs after Thread0 has completed? If we have two threads running on different objects we will get a mix.
Synchronization means only one thread( on single object) can access the method at a time.
how can we create two threads on same object when we are extending a Thread class.