I am trying to implement handlers that listen on the same Looper from different threads.
Below I have two handlers, one of which is created in the main thread, the other in the child thread, however both of them are initialized to listen on the Main Looper.
private Handler mMain;
public static final ThreadPoolExecutor tpe =
(ThreadPoolExecutor) Executors.newCachedThreadPool();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMain = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Log.wtf("", "main:" + msg);
}
};
tpe.execute(new Runnable() {
private Handler tChild = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Log.wtf("", "child:" + msg);
}
};
@Override
public void run() {
Log.wtf("", "send msg to main looper");
tChild.sendEmptyMessage(100);
}
});
}
But when I send the message as shown below, only the child handler prints the message. The main handler does not receive the message.
03-20 22:02:26.754: A/(12857): send msg to main looper
03-20 22:02:26.847: A/(12857): child:{ what=100 when=-8ms }
What am I doing wrong? Thanks for reading.
source
share