This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi 'All.. I was working on synchronization code..but more Iam working on it more Iam getting pl help me.. does running a static synchronized method blocks the thread in running non-static synhronized method or it only blocks the thread in running other static synchronized methods??? what is the situation if the threads belongs to two different objects? thnx rishi
Static locks and instance locks aer very different things. Think of a static lock as a locak that belongs to a class. Therefore, Object has 1 lock. However, you can have 50 instances of Object, each with its own instance lock. Therefore, you'd have 50 instance locks. So what does that mean? Well, let's say you had a bit of code like this:
Since the run method is invoking a static method of a class, once one thread starts executing that method, all other methods that want to invoke that method (or any other static methods of that class) will have to wait. If you execute the above code, you'll get the following output:
It is actually possible for thread 2 to excecute first, but, the important thing to remember is that once one thread starts that method, it will complete it before the next thread starts. Hence, no mixing of the 1's and 2's. However, remove the synchronized keyword and you'll get this outout:
Notice that the 1's and 2's are mixed so both threads are executing that method at the same time. That was earlier prevented with the static keyword. Okay, so what about locking on an instance? Well, let's change our code a little bit to look like this:
Notice that now each thread creates an instance of the Sync object and invokes the instanceLock method on its respective instance. The resulting output looks like this:
There's no synchronization here because each instance of the Sync class has its own instance lock. Since each thread has its own instance of that class, each thread is able to obtain a lock at the same time. However, if we modify the code yet again so that each thread has a reference to the SAME instance of the Sync class...
Now we get output like this:
Now that they're both using the same instance, they have to synchronize. I hope that clears up how this all works. So what happens if one thread invokes a synchronized static method while another invokes a synchronized instance method? No synchronization occurs. That's because the static method operates on the class lock while the instance method operates on the instance lock. I'll leave checking this out as an exercise for you to try - I think I've made this post long enough the way it is. I hope that helps, Corey
Thnx a Lot Corey ur examples cleared my concept ..actually I was trying on single THREAD instead of multiple threads on single OBJECT. what I understood is: threads of same object cannot run nonstatic synchronized methods simultaneously threads of same object cannot run static synchronized methods simultaneously whereas threads of same object can run static and nonstatic methods run simultaneously but for the individual thread the methods will be called according to the order in run method is there anything wrong in there??? rishi
Joined: Dec 20, 2001
It looks like you've pretty well got it, but I think it might be a little easier if you change you thinking a little bit. Rather than thinking about how many threads there are, think of how many locks there are. Therefore, you could have 2 threads or 100 threads but, if they're all locking on the same object, they're going to have to go through 1 at a time.