0

I'm researching how sling framework work behind the sence. But my knowledge on multi thread programming is very limited to understand what they are doing, here's my issue:

This is the class i'm working on AdapterManagerImpl.java, on function registerAdapterFactory (line 247) they defined a synchronized code block at line 281 it is:

        synchronized ( adfMap ) {
            adfMap.put(reference, factoryDesc);
        }

Any one can explain me why adfMap is a shared resource and marked as synchronized ? How does it afftect to performance ?

Tien Nguyen
  • 4,298
  • 9
  • 30
  • 44
  • 1
    I do not know that framework but it seems afMap can be accessed or "used" by different threads concurrently. So you have to protect it against concurrent modification by synchronizing it. While in synchronized block, no other thread can get a lock on it, so they'll have to wait. This affects performance of course but that depends highly on the probability of collisions. The act of synchronization itself introduces an additional overhead regarding performance, too - regardless how many collisions happen. – Fildor Oct 29 '15 at 09:23
  • The code snippet does not _mark_ adfMap. That's a synchronized _statement_. It is executed in three steps: first, adfMap is locked, then the statement (or statements) inside the braces are executed, and then adfMap is unlocked. The main significance of locking an object is that no two threads are ever allowed to lock the same object at the same time. – Solomon Slow Oct 29 '15 at 13:16
  • The synchronized statement causes the first threads which calls this code to aquire the lock object (the one in the round brackets, _adfMap_ here). Now no other thread can aquire this lock (for an lock object identity is used) until the first thread does not hold it any longer. The JVM prevents any thread which does not hold the lock to invoke the code guarded by the lock. Hence no other thread can invoke the code guarded by THIS lock until the lock is freed. However another thread may call the same code (for example on another instance of the class) if the lock refers to a different object. – Sebastian Oct 30 '15 at 08:10

1 Answers1

0

adfMap

is used as a lock.

synchronized ( adfMap ) { ... }

means that no two threads can concurrently call any code guarded by this lock. So simplye - so easy to get it wrong :-).

Informal explanation: When this lock is used as a lock in another class too this affects the corresponding instances of these other classes too.

When this lock is a private static member in AdapterManagerImpl this means that no two threads can concurrently enter any code guarded by this lock on any instance of AdapterManagerImpl.

More formal explanation: A lock guards prevents all code sections guarded by it to be entered by two different threads concurrently (the same thread can enter the code more than once -> synchronization is re-entrant). The lock, which is an object, guards all code sections guarded by this lock object (object identity). If there is another lock variable with an identical name somewhere in the code this is completely independent from the first lock.

Sebastian
  • 395
  • 2
  • 7
  • You don't have enough information to support that claim. Two or more threads _can_ execute the code in the curly braces at the same time if the expression, `adfMap`, yields different object references for the different threads. For example, if the synchronized block appeared in an instance method, and `adfMap` was an instance variable. – Solomon Slow Oct 29 '15 at 13:18
  • @james large: I addressed this issue (by the 'static member') but in a confusing way. Please read answers more carefully next time. Improved explanation. However do not forget that the OP claimed to have only little knowledge in threading - so you should avoid explanations which are so complex that they are of no help to him. – Sebastian Oct 30 '15 at 08:00