File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronizing question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronizing question" Watch "Synchronizing question" New topic
Author

Synchronizing question

Gert Tomas
Greenhorn

Joined: Aug 18, 2007
Posts: 15
Example from Enthuware mock test :

The answer is :
One can be certain that none of the letters i, j and k will ever be printed during execution.

My question is how do you decide what object to synchronize on ? In the above example they synchronized on what seems to me to be an object that has got nothing to do with the rest of the code.
In the above example, could I make an instance of any class I have, lets say for example class Animal and then synchronize on that instead of Object to do the above code ?

I guess I'm a bit confused when to synchronize on an object (and what object) and when to synchronize on 'this'.

Is there some concept I'm not getting here ?
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3216
Note that the lock1 and lock2 are static.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Gert Tomas
Greenhorn

Joined: Aug 18, 2007
Posts: 15
Sorry, but I dont get it. Whats the significance of the fact that they are static ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

Is there some concept I'm not getting here ?


The concept is simple. When two threads are synchronizing on the *same* object, they will block on each other. Which leads us back to...

Sorry, but I dont get it. Whats the significance of the fact that they are static ?


Based on the concept just mentioned, what's the significance? What does the fact that the variable being static do?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gert Tomas
Greenhorn

Joined: Aug 18, 2007
Posts: 15
Mmmm, ok. In the main method there are two instances of TestClass made, so they 'share' the instances of Object that are made because the Objects created here are static, if they were not static then each instance would have its own seperate Objects and they could'nt block each other ?

If I'm correct, then just as a matter of interest, could I use any object instead of an instance of class Object like they did in the example ?
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
Posts: 20
Are you sure of the answer?
well, it seems possible to me that while thread 2 is doing the comparison, thread1 could have increased only 1 of the variables, thereby printing something.

And sure enough, when i tested the code, it printed an endless stream of j's.
Gert Tomas
Greenhorn

Joined: Aug 18, 2007
Posts: 15
Whoops, the correct answer is :
One cannot be certain whether any of the letters i, j and k will be printed during execution.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

Mmmm, ok. In the main method there are two instances of TestClass made, so they 'share' the instances of Object that are made because the Objects created here are static, if they were not static then each instance would have its own seperate Objects and they could'nt block each other ?


Correct.

Are you sure of the answer?
well, it seems possible to me that while thread 2 is doing the comparison, thread1 could have increased only 1 of the variables, thereby printing something.


This is also correct.

Synchronization is cooperative. By having the locks as static, the different threads that increment the variables do block on each other. However, the method that does the check and print does not, and hence, may see intermediate values.

Henry
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
--------------------------------------------------------------------------------
In the main method there are two instances of TestClass made,
so they 'share' the instances of Object that are made because the Objects
created here are static, if they were not static then each instance would
have its own seperate Objects and they could'nt block each other ?



Correct.
--------------------------------------------------------------------------------

I ran the above program without the static modifier for the 2 objects
Object lock1 = new Object();
Object lock2 = new Object();

The output was an endless stream of K's. If being 'static' was the reason the 2 threads blocked each other , why is this happening?
Thanks,
Meera
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Synchronizing question
 
Similar Threads
Thread
Synchronized blocks vs methods.
synchronization--->JQ+
Help.....Thread
Question related to Thread synchronization.