• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized (Help!)

 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When specifying an object method with the synchronized keyword, is the entire object locked or can non-synchronized methods still be invoked?

Also, when using synchronization at the block level:

synchronized (someObject) {}

, is the entire object locked or can non-synchronized methods still be invoked?

Thanks.
 
J Kneeland
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do believe that when you lock an object using the sychronized(objname) syntax , it just prevents other threads from locking on it. Threads do not have to lock on to an object to alter it and this may cause trouble. For the first one, why don't you try it. Make a sychrnoized method that keeps printing out "sych" and an unsychronized method that prints out "un-sych". Invoke each methos in a different thread. It should only print the text of the first method called if it really locks the object.
 
Brian Knight
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you synchronize on an object, all other methods that are not marked as synchronized will still be able to run, however any threads trying to run synchronized methods will have to wait for the lock to be released. Synchronizing on an object doesn't lock the object in the way you are thinking. It means that the currently executing thread obtains that objects lock and other threads that need to execute the code have to wait for that lock to be released, before they can try to obtain the lock. If the code is not synchronized, the threads don't need to obtain the lock so the code executes
 
Alan Walker
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the object you lock on is an instance of a thread-safe class such as Vector, I think this will prevent any other thread from executing methods of that object, but this is because the methods of these classes are already synchronized.

So while a block



is executing, it is not possible for a statement such as



to execute in another thread.

Perhaps this is what leads some writers to talk about "locking objects", as if using an object as a monitor automatically protects it from any alteration. For the sake of clarity, it is probably better to think of the object you synchronize on as simply being a token. When you need to prevent methods from running concurrently, you should explicitly code for that using synchronized blocks.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: You are not locking the object. You are using the object as a lock or 'monitor' for a particular piece of code. The object that you are using for your lock is not affected in anyway by the action of locking. it can still be accessed as before generally.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!