Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:14:33+00:00 2026-05-26T01:14:33+00:00

I am doing an Android service that gives content to other apps that can

  • 0

I am doing an Android service that gives content to other apps that can register as callback.

I am not 100% sure about how the Android Handler class works, so can someone confirm me 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()) {

                // Blocking call
                byte[] message = JniCommunicator.readMessage();

                if (message == null || message.length == 0) {
                    continue;
                }

                Bundle b = new Bundle();
                b.putByteArray(MESSAGE, message);
                Message m = readHandler.obtainMessage(REPORT_MSG);
                m.setData(b);
                readHandler.sendMessage(m);
            }
        }
    };

    private final Handler readHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
            case REPORT_MSG:

                byte[] message = msg.getData().getByteArray(MESSAGE);

                // Broadcast the new message to all clients
                final int N = readerCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        readerCallbacks.getBroadcastItem(i).newMessage(message);
                    } catch (RemoteException e) {
                        // The RemoteCallbackList will take care of removing
                        // the dead object for us.
                    }
                }
                readerCallbacks.finishBroadcast();

                break;
            }
        }
    };

        @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IService.Stub mBinder = new IService.Stub() {

        public void registerCallback(IMyCallback cb) {
            if (cb != null)
                readerCallbacks.register(cb);
        }

        public void unregisterCallback(IMyCallback cb) {
            if (cb != null)
                readerCallbacks.unregister(cb);
        }
    };
}

In particular, if someone calls unregisterCallback() while the Handler is in the for loop, will it crash?

From my understanding, the Handler run in the same thread, so it is thread safe, but I am not sure.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:14:34+00:00Added an answer on May 26, 2026 at 1:14 am

    Handlers are thread safe, that is their entire purpose.
    I’ll agree that the documentation on the thread safety of handlers isn’t the best but it would be very ironic if a class designed to communicate between thread weren’t thread safe.

    About the remote callbacks, they are also designed to be thread safe, you should read the documentation on this, it states clearly:

    Performs locking of the underlying list of interfaces to deal with multithreaded incoming calls, and a thread-safe way to iterate over a snapshot of the list without holding its lock

    All you have to make sure is that all variables multiple thread access are thread safe (which they are in your case) and that they aren’t being changed (yours are final so no worries there either)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now i am doing an android application.In that application I am using asynchtask class
I'm doing some initial research on smart phone development, and I noticed that Android
i have alarm service that listin to android.intent.action.PHONE_STATE and it work good but when
I don't know if i'm doing it right or not. I created a service
I have an Android application that is binding to a persistent service (once started
I'm working on a web service that will be accessed from an Android app.
What is the right way of doing foreground service which I can later bind
I have an android app which runs a foreground service and while that service
I have WCF Service and my Android application consumes it. I am doing this
I'm trying to start doing Android development on OS X (trying is the key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.