K
klem s
1) Assuming instance methods ( M1 and M2 ) defined by same class both
have locks around the code manipulating data and also assuming the two
methods don’t operate on same data, then wouldn’t it be best if M1 and
M2 used different identifiers as their lock tokens, since that way
threads T1 and T2 calling M1 and M2 respectively won’t get blocked.
2) I assume private reference used as a token should be declared as
instance member when only methods within the same instance will share
same data, but if methods within different instances will share data,
then reference should be declared static? Thus wouldn’t a static token
reference make multiple instances of same class thread safe with
respect to each other?
3) We usually use internal members for lock tokens, so I would assume
that we shouldn’t try to synchronize different classes accessing same
data, but instead we should dedicate a single class D ( its methods
would implement locks ) for accessing particular data and any class
trying to operate on that piece of data should access data only
through D?
4) I realize that we should use private members as lock tokens, since
if they were public, then external code may also use them as lock
tokens, which may lead to deadlocks.
But is there a situation where it would make sense for two methods
( defined in different classes ) to use same reference as a lock
token? Perhaps when both methods operate on same data and thus the
two methods having same reference for a lock would help us synchronize
access to that data?!
5)
a) As far as I can tell, synchronization between different methods of
the same class C should be used in the following circumstances:
• when methods are manipulating same members of that class
• when methods are operating on the same external data ( such as
files etc )
b) As far as methods operating on same data members are concerned,
should we:
• should we always implements locks inside those methods
• or only if we suspect C will be used in a multithreaded application
• or should we let the client code using C to provide a proper
synchronization?
My guess is that we should let the client code using C to provide for
thread safety, but in what situations should we instead make C class
thread safe?
Thank you
have locks around the code manipulating data and also assuming the two
methods don’t operate on same data, then wouldn’t it be best if M1 and
M2 used different identifiers as their lock tokens, since that way
threads T1 and T2 calling M1 and M2 respectively won’t get blocked.
2) I assume private reference used as a token should be declared as
instance member when only methods within the same instance will share
same data, but if methods within different instances will share data,
then reference should be declared static? Thus wouldn’t a static token
reference make multiple instances of same class thread safe with
respect to each other?
3) We usually use internal members for lock tokens, so I would assume
that we shouldn’t try to synchronize different classes accessing same
data, but instead we should dedicate a single class D ( its methods
would implement locks ) for accessing particular data and any class
trying to operate on that piece of data should access data only
through D?
4) I realize that we should use private members as lock tokens, since
if they were public, then external code may also use them as lock
tokens, which may lead to deadlocks.
But is there a situation where it would make sense for two methods
( defined in different classes ) to use same reference as a lock
token? Perhaps when both methods operate on same data and thus the
two methods having same reference for a lock would help us synchronize
access to that data?!
5)
a) As far as I can tell, synchronization between different methods of
the same class C should be used in the following circumstances:
• when methods are manipulating same members of that class
• when methods are operating on the same external data ( such as
files etc )
b) As far as methods operating on same data members are concerned,
should we:
• should we always implements locks inside those methods
• or only if we suspect C will be used in a multithreaded application
• or should we let the client code using C to provide a proper
synchronization?
My guess is that we should let the client code using C to provide for
thread safety, but in what situations should we instead make C class
thread safe?
Thank you