I use the Android service, which provides content to other applications that can register as a callback.
I am not 100% sure about how the Handler class for Android works, so can someone confirm that this code is thread safe?
public class MyService extends Service { private static final String MESSAGE = "message"; private final RemoteCallbackList<IMyCallback> readerCallbacks = new RemoteCallbackList<IMyCallback>(); private static final int REPORT_MSG = 1; private Thread readerThread; @Override public void onCreate() { readerThread = new Thread(readerRunnable); readerThread.setDaemon(true); readerThread.start(); } private Runnable readerRunnable = new Runnable() { @Override public void run() { while (!Thread.interrupted()) {
In particular, if someone calls unregisterCallback () while the handler is in a for loop, will it fail?
From my point of view, the handler works in one thread, so it is thread safe, but I'm not sure.
thanks
source share