마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

synchronized사용에 대해서 문의합니다.

0 추천
안녕하세요.

 

답변주시는 여러분들께 진심으로 감사말씀 미리 드리겠습니다 ^^

 

다름이 아니고,

DeadLock현상이 발생했는데

해당 루틴이  DeadLock을 유발할수 있는 가능성이 있는지 문의 합니다.

 

Object mLock = new Object();

...

 

public void methodA()
{
    synchronized(mLock)
    {
        methodB();
    }
}

...

public void methodB()
{
    synchronized(mLock)
    {
         .....
    }
}

즉, methodA는 mLock을 synchronized걸고 있는데

methodB에서도 mLock을 synchronized를 겁니다.

 

해당 방법이 DeadLock유발 가능성이 있는 건지 궁금합니다.

 

 

미리 답변 감사드립니다 ^^
레시이 (150 포인트) 님이 2013년 3월 11일 질문

1개의 답변

+1 추천

synchronized keyword 설명의 가장 하단을 보면,

http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

같은 thread 내에서는 문제 없군요. 재접근적(reenterant)이라고 하네요. 말씀하신 부분에서 dead lock 은 발생하지 않겠군요. 

Reentrant Synchronization

Recall that a thread cannot acquire a lock owned by another thread. But a thread can acquire a lock that it already owns. Allowing a thread to acquire the same lock more than once enables reentrant synchronization. This describes a situation where synchronized code, directly or indirectly, invokes a method that also contains synchronized code, and both sets of code use the same lock. Without reentrant synchronization, synchronized code would have to take many additional precautions to avoid having a thread cause itself to block.

상인 (6,670 포인트) 님이 2013년 3월 11일 답변
...