为什么我无法在android中停止我的线程

时间:2014-06-15 15:44:23

标签: java android multithreading

我有一个线程类:

private class Mythread extends Thread{
       boolean mfinish = false;
       private void finish(){
           mfinish = true;
           interrupt();
       }
       while(!mfinish){ do my work}
}

我使用SparseArray<Mythread> mThreadArray来保存线程。

我在我的服务A:

中注册了BroadcastReceiver
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver(){
         public void onReceive(Context context, Intent intent) {
          if(intent.getAction().euqals(STOP_THREAD)){
               uid = bundle.getInt("uid");
               pid = bundle.getInt("pid");
               tid = bundle.getInt("tid");
               if(mThreadArray.get(tid)==null){ 

                  Mythread samplThread = new Mythread(pid, tid);
                  mThreadArray.put(tid,sampleMainThread);
                  sampleThread.start();
              }
          }
          if(intent.getAction().equals(STOP_THREAD)){
              uid = bundle.getInt("uid");
               pid = bundle.getInt("pid");
               tid = bundle.getInt("tid");
               if(mThreadArray.get(tid)!=null){ 

                  mThreadArray.get(tid).finish();
                  mThreadArray.remove(tid);
              }
          }

    }

}

在另一个应用程序B中,创建或结束有线程时sentBroadcast()。 但是,在我的服务A中,我发现当&#34;停止&#34;时,不能停止创建的线程。意图来了。

有没有人知道原因?

0 个答案:

没有答案