java - mHandler isn't set from within a synchronized block in Android SDK -
looking through android sdk framework source code, i've come across this:
private final class gpslocationproviderthread extends thread { public gpslocationproviderthread() { super("gpslocationprovider"); } public void run() { process.setthreadpriority(process.thread_priority_background); initialize(); looper.prepare(); mhandler = new providerhandler(); // signal when initialized , ready go minitializedlatch.countdown(); looper.loop(); } }
(this froyo's frameworks/base/location/java/com/android/internal/location/gpslocationprovider.java)
gpslocationproviderthread
inner class of gpslocationprovider
, , mhandler
member instance variable of gpslocationprovider
. variable set within thread's run()
method, no synchronization applied, , mhandler
not volatile
.
why work? , if 99% of time work, it's not guaranteed work, , it's not practice. correct in understanding, or there subtle code i'm misunderstanding?
the thread created when provider created (in constructor), , minitializedlatch
makes sure constructor proceeds once thread , running.
after point, mhandler created , valid, , since handler class' methods reentrant, system should thread-safe. handler, after all, class designed inter-thread communication.
Comments
Post a Comment