I have a synchronised method A() that is taking lock on class, and calling two other unsynchronised methods B() and C() in its body. My question is while control is moving from A() to B() will it release the lock on class and will it be reacquired when control come back after execution of B finishes.

synchronised A(){
      //will lock be released here?
      b();
      //will lock be reacquired here?
      c();
}

B(){
   will lock be available here?
//do somthing
}

C(){
//do something
}

Please help. Thanks in advance.

有帮助吗?

解决方案

The only time a lock gets released and reacquired while within a synchronized block is when wait is called. Otherwise the lock is held until the block is exited.

If other threads are calling B or C then those will not be affected by any locking on A.Any thread calling B from A will still have its lock, but that doesn't matter to threads calling B without coming through A, they will not stop to acquire a lock.

其他提示

The scope of the synchronized block is the method. method calls which occur within A() are part of that methods scope, so no. This is not to say that the calling thread of A() and some other thread couldnt both be in b() or c() concurrently, but the lock will not be released on method A().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top