-1

Hi my question is how synchronization works?

In simple words we know that if a thread entered in a synchronization block by acquiring lock on any reference, than no other thread acquire that lock until first one exit from synchronized block.

But my question is if the thread acquired a lock on a reference and execute methodA() in that method there is a synchronized block, than can other thread acquire a lock on same reference and execute methodB(), there is also a synchronized block in it?

Anant
  • 109
  • 2
  • 5
  • 11
  • 1
    The method or block doesn't matter. The synchronization target object matters. – Sotirios Delimanolis Feb 04 '14 at 17:56
  • 1
    You don't lock on reference but on monitor of object which reference holds. – Pshemo Feb 04 '14 at 17:56
  • Are the method invocations inside a `synchronized`, with more `synchronized` blocks inside, or is there synchronization just inside methods A and B? – afsantos Feb 04 '14 at 18:02
  • there is just synchronized block inside method A and B. no sub sequence call from that synchronize block. – Anant Feb 04 '14 at 18:06
  • @SotiriosDelimanolis well method and block matter, because when we use synchronize keyword on a method level than it acquire a lock on the object on which the method is invoked. so in that case if two threads that hold different object of that class can execute different even same synchronized method. – Anant Feb 04 '14 at 18:08
  • Don't think of the method or the block, think about what object is being used. That's all that matters. – Sotirios Delimanolis Feb 04 '14 at 18:10
  • I guess some code would help here.. – Narendra Pathai Feb 04 '14 at 18:11

3 Answers3

0

I believe that the second lock will have to wait till the first lock has been released.

Have a look at the link Java synchronized references for more information.

Community
  • 1
  • 1
clinomaniac
  • 2,200
  • 2
  • 17
  • 22
0

Synchronization is very simple

One Thread locks any number of objects.

Only one thread can lock a given object at a given time.

When attempting to lock an object that is already locked by another thread, a thread has to wait until the object is released.

A Thread in wait state releases the locks it holds until it exists the wait state (at which time it attempts to reacquire the locks previously held).

In java, a lock is acquired using a synchronized block.

To answer your question: 2 threads can never acquire a lock on the same object at the same time.

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • Ok, than i have a issue. synchronized(b){ try{ b.wait(); }catch(InterruptedException e){} now as i acquired a lock on b object, it means no other thread can acquire a lock on b object. so maximum one thread can be in waiting state, than why there is a notifyall method, because maximum one thread can be in waiting state than there should be notify only. – Anant Feb 04 '14 at 18:14
  • As I mentioned: `A Thread in wait state releases the locks it holds` (otherwise you could never call `b.notify()`, since it requires to hold the lock on `b`) – njzk2 Feb 04 '14 at 18:25
0

Synchronization is for mutual exclusion

Whenever you synchronize on an object, a lock is obtained on the monitor of that object.

Image Source: Thread synchronization

enter image description here

As the image shows as whenever a thread acquires a lock on monitor then it becomes the owner thread and no other thread can obtain lock on same monitor unless the owner thread enters wait state or releases the lock.

That said another point to keep in mind is that locks that are used in synchronized blocks are Reentrant, which means that if Thread 1 is the owner of the lock and same thread again tries to gain lock of which it is owner then Java will allow that.

Ok, than i have a issue. synchronized(b){ try{ b.wait(); }catch(InterruptedException e){} now as i acquired a lock on b object, it means no other thread can acquire a lock on b object.

On calling wait(), the owner thread releases the lock and goes in Wait Set as shown in diagram. After that someone else from Entry set can get the lock.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120