• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchroniztaion question  RSS feed

 
Vinu krish
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Can someone explain the significance of the following piece of code.
String a = new String();
synchronized(a)
{
some code
}
Wht exactly happens? I have heard of synchronizing a block of code and methods. But here I am passing a string object ( or rather it could be an object of a class also). Pls help as I am preparing for an interview
Thanks,
Vinu
 
Jonathan Locke
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code is pretty nonsensical for a couple of reasons:
1. It's customary to use java.lang.Object when creating a lock object.
2. The String isn't held in a member variable. It's apparently a local variable, which would mean each thread would create its own lock object and therefore always get access to it immediately, defeating the whole purpose of lock objects.
What you *mean* to write is more like this:

NOTE: method1 is just a shorthand way of doing exactly what method 2 does.
There's a lot more to locks and monitors than this, but does this at least make sense now?
 
Vinu krish
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jonathan. I wanted to try with an object only. Since I have never used synchronization i didnt know abt tht!! But if a thread is in a synchronized block, say as per ur eg, synchronized (this) of method2(), then can another thread access other non-synchronized methods of Foo class? Pls tell me if what i say is correct. Can ANother instance of Foo access all the methods?
What is the significane of synchronizing on Object lock? I can create an instance of Foo(say abc) and then can I do this? synchronized(abc) . Does this mean that, thread of abc cannot access the other synchronized methods of Foo? Sorry if I am sounding too silly!! I have not worked.. Can u suggest some nice document which can be of some help to me
Thanks again
Vinu
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. synchronization lock only applies to those statements with synchronize around them and only with respect to that lock object.
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan,
Could you explain the differences between synchronized(this) and synchronized(lock)? 'this' is an object instance, whereas lock is a member instance. Does it mean synchronizing on the lock member object allowing other threads to access the object, not the piece of code within the synchronized block? On the other hand, synchronize(this) blocks the whole object instance; therefore, preventing other threads to access the object. I guess my question is when I should use synchronized(this) and when I should use synchronized(lock).
Thanks
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronization is a courteous gesture. It is not forced upon a thread. The thread must check for itself the lock.
the only thing synchronization does is guarantees that any thread must first get the lock for the synch object before it is allowed into the synchronized portion of code.
using 'this' or some other object reference works fine. But you should get in the habbit of using
Object key = new Object();
for your lock/key object as opposed to using an object which has another purpose in life.
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for your lock/key object as opposed to using an object which has another purpose in life.

Could you elaborate on this? What purpose is it?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object that appears in the parenthesis is ONLY use for a lock, access to that object is not affected at all.
synchronized(objectA)
has absolutely no direct effect upon access to objectA's methods. objectA is just being used as a token so those interested threads can make sure only one of them accesses the critical section at a time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!