0

I have a thread class:

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

and I use SparseArray<Mythread> mThreadArray to hold threads.

I registered a BroadcastReceiver in my service A:

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);
              }
          }

    }

}

In another application B, sentBroadcast() when there is a thread is created or ended. However, in my service A, I found the created threads can't be stopped when the "stop" intent comes.

Does any body know the reason?

Sufian
  • 6,405
  • 16
  • 66
  • 120
Jansen
  • 133
  • 12
  • 1
    Never use `==` to compare Strings. Always use `equals(...)` or `equalsIgnoreCase(...)`. – Hovercraft Full Of Eels Jun 15 '14 at 15:45
  • 1
    As an aside, you usually will want your class to implement Runnable and not to extend Thread. – Hovercraft Full Of Eels Jun 15 '14 at 15:47
  • 1
    Also, shouldn't `intent.geAction` actually be, `intent.geAction()`? Something like, `if ("stop".equalsIgnoreCase(intent.getAction()) {` Also, I'd use a String constant, `public static final String STOP = "stop";` so as to avoid any problems with misspelling. – Hovercraft Full Of Eels Jun 15 '14 at 15:52
  • I wrote my code manually to the Edit box so I made some spelling problem. But the code on My IDE is OK. The radical problem is not the String Comparing but the threads created when the start intent comes can't be stopped – Jansen Jun 16 '14 at 01:15

0 Answers0