• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization  RSS feed

 
Somu Sundaram
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have some questions in my mind.

1. Whats the difference between the synchronized keyword when applied to a method and a block of code.

2. When synchronization is done, a lock is obtained to a particular object only. In that case the same synchronised method can be accessed by multiple threads with different instances of that class. Suppose if that synchronized method accesses a shared resource, how thread-safe is achieved. If not why this short fall(?).

I have not coded to validate this questions of mine. While I am reading these things came to my mind. So I am posting them. If anybody of u can help me pl post a message.


Thanks and Regards,
S Somu
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The answer to "1" is "no difference." If you enclose the entire body of a method in a synchronized block, it's exactly the same as if the method were marked "synchronized" in its declaration.

The answer to "2" is that the multiple threads may need to synchronize on an object controlling the shared resource. If the shared resource is a group of static member variables (for example), then they should access the variables by calling a synchronized static method. Such a method is synchronized on the unique java.lang.Class object that represents the class the method is in.
 
Somu Sundaram
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernst,
Thanks for ur reply.
----------------------------
The answer to "2" is that the multiple threads may need to synchronize on an object controlling the shared resource. If the shared resource is a group of static member variables (for example), then they should access the variables by calling a synchronized static method. Such a method is synchronized on the unique java.lang.Class object that represents the class the method is in.
-----------------------------
But I am still not cleared of the doubt. Suppose that synchronized method is accessed on an object, the same synchronized method can be accessed by multiple threads with different objects of the same class.(Am I right here?). In that case, data corruption is possible as multiple threads is accessing a shared resource(do not consider it as a static var if it contradicts here)simultaneously from different threads in different objects.

Correct me if I am sounding absurd.


Thanks and Regards,
S Som
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somu,

Each class in java has to locks associated to it. The class lock that is one for all instances of the class and the object lock that is unique to each instance of the class.

When you synchronize on the instance of a class, u get the objects lock.
If you synch on the static method (class method) then u get the class lock.
Those are 2 different locks and 2 threads can work with the same object as long as they hold different locks. So if T1 gets class lock then all synch static methods will be locked but thread T2 can still access not static synch method. Now, if T1 will have to get objects lock to execute any non static synch methods and while T2 holds the object's lock, T1 will have to wait.

U can corrupt data that is shared in both methods.


[ July 08, 2004: Message edited by: Yaroslav Chinskiy ]
 
Roshan Lal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Somu Sundaram:
Suppose that synchronized method is accessed on an object, the same synchronized method can be accessed by multiple threads with different objects of the same class.(Am I right here?).

You are on right track.

In that case, data corruption is possible as multiple threads is accessing a shared resource(do not consider it as a static var if it contradicts here)simultaneously from different threads in different objects.

How? When multiple threads access same method with different instances of same class they have different locks (one for each instance) and those instances have their own resources.
I am not quite clear on your doubts, may be if you give a concrete example
it will help.


My two cents
-Roshan
 
Somu Sundaram
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roshan Lal,
What I mean as a shared resource is a file in the hard disk. If a sync method is trying to access this file, I hope data corruption will be there due to multiple thread access. This will be averted by obtaining a lock on the class. instance lock may not help here.

Now, can this situation be considered for synchronization?

Thanks Yaroslav Chinskiy for providing me a nice explanation.


Thanks and Regards,
S Somu
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somu,

There are many ways to synch file access. U only need to synch it if you do writes/reads by more than 1 thread. U can have singleton File object (or just an Object) that will work as a lock. So any thread will have to get the lock before reads/writes. U can provide one class that handles writes/reads and have multiple threads use it (write/read methods will be synched). U can have
a synched queue where all threads will write and 1 worker thread that will
flush it to the file. If it's a producer/consumer problem then you can use wait/notify + the lock object.

Class lock is just another lock assotiated with every instance of that class.
Like I said 2 threads may access shared recource if they compete for different locks. The implementation of synch will depend on the req. of the problem.
 
Somu Sundaram
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yaroslav Chinskiy,
Thank u once again for ur xplanation.


Regards,
S Somu
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!