为什么线程的id改变了

时间:2017-12-17 18:15:21

标签: android multithreading

我不明白为什么我在同一个帖子中得到不同的ID就是输出 我知道任何编程语言中的id字段必须保持不变以供将来参考
PS:这段代码在绑定的服务类中,我知道绑定的服务在主线程上,但为什么ID为"消息线程"从-1变为21947变为41045

3次
*logcat output:
    onCreate: thread id = 1 name =main
    first is alive true thread id -1 thread name =servicemessanger
    sec is alive true thread id 21947 thread name =servicemessanger
    handleMessage: thread id= 41045name = servicemessanger* 




code :
     @Override
        public void onCreate() {
            Log.i(TAG, "onCreate: thread id = "+ Thread.currentThread().getId()+" name =" +Thread.currentThread().getName()  );
            super.onCreate();
            HandlerThread thread = new HandlerThread("servicemessanger");
            thread.start();
            Log.i(TAG, " first is alive "+thread.isAlive() + " thread id "+ thread.getThreadId()+ " thread name ="+thread.getName());
            mHandler = new Handler(thread.getLooper()){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    Log.i(TAG, "handleMessage: thread id= "+Thread.currentThread().getId()+"name = " + Thread.currentThread().getName());
                }
            };
            mHandler.sendEmptyMessage(0);
            Log.i(TAG, " sec is alive "+thread.isAlive() + " thread id "+ thread.getThreadId()+ " thread name ="+thread.getName());

        }

谢谢,

0 个答案:

没有答案