I have searched the java API on Thread, Handler, HandlerThread, Looper , as well as SO, and cannot find an answer.  Of course, I have also used Eclipse's debugger to step and inspect my 'msg'.

Situation: If you protect your data model from concurrency issues by allowing only one worker thread to have access to it, how would you then differentiate between a Runnable versus a Message in its handleMessage(Message msg) if you were enqueueing both Messages and Runnables in the same message queue?

Q. While my Runnables execute as planned after being posted via handler.postDelayed(myRunnable), I would like to inspect the contents of the Runnable while my worker thread is switching upon it so that I can update my data model appropriately.  

I welcome all feedback and appreciate your time!

Below is the entire contents of 'msg' as shown from Eclipe's debugger.  Is there even enough information here to switch on the Runnable?

msg Message  (id=830030887816)  
    arg1    0   
    arg2    0   
    callback    null    
    data    Bundle  (id=830030968104)   
        mAllowFds   true    
        mClassLoader    BootClassLoader  (id=830023329296)  
            packages    HashMap  (id=830023329320)  
            parent  null    
        mFdsKnown   true    
        mHasFds false   
        mMap    HashMap  (id=830030980304)  
        mParcelledData  null    
    flags   1   
    next    null    
    obj null    
    replyTo null    
    target  IncomingHandler  (id=830031033176)  
    what    0   
    when    180369888   


Below is my worker thread's handleMessage().  Specifically, I instantiated a HandlerThread as my worker thread and bound it's getLooper() to my worker thread Handler.

@Override
public void handleMessage(Message msg) {

    // If this is a Runnable then I need to call my method.
    // This next line is a hack to test for Runnable.
    // I would like to know how to inspect this Runnable
    if (0 == msg.arg2 &amp;&amp; 0 == msg.arg1 &amp;&amp; 0 == msg.what)
        incrementStateMachineBecauseRunnableStarted();

    // Determine which action to take
    // http://stackoverflow.com/questions/5021246/conveniently-map-between-enum-and-int-string
        switch (Cmd.values()[msg.what]) {

        case RECEIVE_NEW_LIST_EMPLOYEES:
            receiveNewListEmployees(msg);
            break;

        case UPDATE_ONE_EMPLOYEE_STATE:
            updateOneEmployeeState(msg);
            break;

        default:
            Log.e(this.getClass().getName(), "unexpected message.";
            break;
}


This is how I enqueue my Runnable (I run this code on my worker thread, so it is sending a message to itself, to be processed later in handleMessage(Message msg)):

            // Create a new Runnable for this employee
            PostUpdate update = new PostUpdate(employee);
            // Post this Runnable with a sort delay
            postDelayed(update, WAIT_BEFORE_PUSH_MS);


... and this is how I enqueue my Messages (from another thread):

                Message msg = mHandler.obtainMessage();
                msg.what = Cmd.RECEIVE_NEW_LIST_EMPLOYEES.ordinal();
                msg.arg1 = 77;  // deliberately inserting debug value
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList(Cmd.RECEIVE_NEW_LIST_EMPLOYEES.toString(), mEmployees);
                msg.setData(bundle);
                mHandler.sendMessage(msg);


Finally, my Runnable:

private class PostUpdate implements Runnable {

    private final Employee mEmployee;

    public PostUpdate(Employee employee) {
        mEmployee = employee;
    }

    @Override
    public void run() {
        // Post update through network to MySql server
        mDao.setState(mEmployee);
    }
}



Question: native-activity sample project won't compile if I change file exteension to .cpp
Answer: Check your Android.mk file looking for main.c and change it to main.cpp

LOCAL_SRC_FILES := main.c


must be:

LOCAL_SRC_FILES := main.cpp