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?
Hi I think the point is you're not obtaining what you want because you are not synchronizing the same object, but two different ones. That means that thread a's myMethod will be locked for every object accessing it (that is, just a itself), and the same goes for b's myMethod. Two different instances = two different locks. Try with this (a little strange, I know):
That means, two different threads accessing the same Runnable instance. Hope this helps Regards LR [ December 10, 2007: Message edited by: Luca Romanello ]
Joined: Jun 04, 2007
Thanks a lot, Luca. And finally, I am understanding this... Thanks again...