2015-12-02 177 views
0

我正在开发具有服务类的Android应用程序。我服务课,我必须开始一个线程。它工作正常。但我需要停止线程。我曾与Android:停止线程

mThread.destroy(); 
mThread.stop(); 

使用,但都被弃用,我已经使用

mThread.interrupt(); 

,但它仍然无法正常工作。

代码

public void Receivepatientattributes(byte[] readBuf, int len) { 

    if (readBuf[8] == 6){ 

       pktcount++; 

       byte pkmsb = readBuf[11]; 
       byte pklsb = readBuf[10]; 
       int pkresult = (pkmsb << 8) | ((int)pklsb & (0x00FF)); 

       System.out.println("Packet : "+pktcount+" : "+pkresult); 
       if(pkresult == pktcount){      

        for(int i = 0, j = 14; i < len ; i++, j++) { 
         queue.add(readBuf[j]); 
        } 
        if(mThread.getState() == Thread.State.NEW) { 
         mThread.start(); 
         System.out.println("Start Thread "); 

        } else { 
         System.out.println("No Thread Found"); 
        } 

       } else { 
        displayToast("Packet Count Error"); 
       } 
     } 
    } 
} 
class CommunicationThread implements Runnable { 

    @Override 
    public void run() { 

     do{ 
      if(queue.getFirst() == (byte)0x80) { 
       System.out.println("absolu1 " + queue.getFirst()); 
       getabs_data(); 

      } 
     } while (queue.size() > 132); 

     if(mThread.getState() == Thread.State.RUNNABLE) { 
      mThread.interrupt(); 
     } 
    } 
} 
+0

http://www.ibm.com/developerworks/library/j-jtp05236/是一个很好的文章WRT正确中断线程。 – zapl

回答

0

尝试用

你的循环中。

+0

“试试这个”并不是很有帮助,因为它没有解释什么是错的,或者如何在另一种情况下解决它。 –

0

Java没有提供任何方法来安全地停止一个线程,它仅提供中断

可以发送拦截到一个线程但这并不意味着要杀死一个线程只有当目标线程能够响应此拦截时,它才能自行停止。

例如,目标有一个循环来检查是否中断。或者,如果目标线程调用了一些可中断的阻塞函数,则可以检查catch块中的中断并停止该线程。

有关Java并发性的信息,请阅读本书《Java Concurrency in Practice》

0

Thread对象是由系统,可以对其进行修改您的应用程序之外的控制。出于这个原因,您需要在中断它之前锁定线程的访问权限,方法是将访问权限放入同步块中。

ublic class PhotoManager { 
public static void cancelAll() { 
    /* 
    * Creates an array of Runnables that's the same size as the 
    * thread pool work queue 
    */ 
    Runnable[] runnableArray = new Runnable[mDecodeWorkQueue.size()]; 
    // Populates the array with the Runnables in the queue 
    mDecodeWorkQueue.toArray(runnableArray); 
    // Stores the array length in order to iterate over the array 
    int len = runnableArray.length; 
    /* 
    * Iterates over the array of Runnables and interrupts each one's Thread. 
    */ 
    synchronized (sInstance) { 
     // Iterates over the array of tasks 
     for (int runnableIndex = 0; runnableIndex < len; runnableIndex++) { 
      // Gets the current thread 
      Thread thread = runnableArray[taskArrayIndex].mThread; 
      // if the Thread exists, post an interrupt to it 
      if (null != thread) { 
       thread.interrupt(); 
      } 
     } 
    } 
} 
... 

}

供进一步参考使用该link

0

CommunicationThread,有一个do ... while循环。编辑这个循环中加入isInterrupted()到环路的病情稍有:

do{ 
     if(queue.getFirst() == (byte)0x80) { 
      System.out.println("absolu1 " + queue.getFirst()); 
      getabs_data(); 

     } 
    } while (mThread.isInterrupted() && queue.size() > 132); 
0

这不是在Android中使用线程是一个好主意。因为它不是线程安全的。相反,请使用AsyncTask,这是为此目的而提供的android sdk。 仅供参考,用于取消的AsyncTask你应该叫:

myAsyncTask.cancel(true); 
+1

不真实/过于简单。 'AsyncTask'本身不过是使用普通的''Thread''的更好的方法。当你实现他们的模式时,他们是首选的:从主线程中分离出来的任务需要报告回主线程。 'AsyncTask'为主线程和后台线程之间的来回提供了一个很好的抽象。他们不添加任何魔法线程安全。 – zapl