안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요~
소스를 보던중...
BroadcastReceiver 를 상속받은 class 의 member 함수가 아래와 같은것을 보았습니다.
public class SmsReceiver extends BroadcastReceiver {
static PowerManager.WakeLock mStartingService;
...
public static void beginStartingService(Context context, Intent intent) {
synchronized (mStartingServiceSync) {
if (mStartingService == null) {
PowerManager pm =
(PowerManager)context.getSystemService(Context.POWER_SERVICE);
mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"StartingAlertService");
mStartingService.setReferenceCounted(false);
}
mStartingService.acquire();
context.startService(intent);
}
}
...
}
흠... synchronized () 는 runable 인터페이스를 사용해야 하는것으로 찾아지는데용.. ? (맞나용?)
그럼 BroadcastReceiver 는 기본적으로 runable 인터페이스를 사용? 한다는..???? (말하면서도 해괴하네요.. ㅡㅡa;;)
아니면... 내부에 사용하는 mStartingService 를 사용하고 있어서 사용가능한??? (이건또 무슨말인가?? ㅡㅡ;;;;)
이런 소스코드를 처음봐서...
어떻게 이해해야 하나요?
고수님들~ 궁금해요~ 궁금해~~~
소스를 보던중...
BroadcastReceiver 를 상속받은 class 의 member 함수가 아래와 같은것을 보았습니다.
public class SmsReceiver extends BroadcastReceiver {
static PowerManager.WakeLock mStartingService;
...
public static void beginStartingService(Context context, Intent intent) {
synchronized (mStartingServiceSync) {
if (mStartingService == null) {
PowerManager pm =
(PowerManager)context.getSystemService(Context.POWER_SERVICE);
mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"StartingAlertService");
mStartingService.setReferenceCounted(false);
}
mStartingService.acquire();
context.startService(intent);
}
}
...
}
흠... synchronized () 는 runable 인터페이스를 사용해야 하는것으로 찾아지는데용.. ? (맞나용?)
그럼 BroadcastReceiver 는 기본적으로 runable 인터페이스를 사용? 한다는..???? (말하면서도 해괴하네요.. ㅡㅡa;;)
아니면... 내부에 사용하는 mStartingService 를 사용하고 있어서 사용가능한??? (이건또 무슨말인가?? ㅡㅡ;;;;)
이런 소스코드를 처음봐서...
어떻게 이해해야 하나요?
고수님들~ 궁금해요~ 궁금해~~~




그냥 다수의 스레드가 beginStartingService 메소드를 호출할 수 있기 때문에...
그리고 이것땜시 나오는 race condition문제 때문에 동기화 시킨거죠....
임의의 메소드 어떤곳에서도 synchronized를 사용할 수 있습니다. 필요가 없는곳에서는 안쓰겠죠....