原贴地址:http://stackoverflow.com/questions/680180/where-to-stop-destroy-threads-in-android-service-class



62
down vote accepted

There are several problems in the code sample you posted I will address in order:

1) Thread.stop() has been deprecated for quite some time now, as it can leave dependent variables in inconsistent states in some circumstances. Seethis Sun answer pagefor more details. A preferred method of stopping and starting a thread is as follows:

private volatile Thread runner; public synchronized void startThread(){  if(runner == null){   runner = new Thread(this);   runner.start();  } } public synchronized void stopThread(){  if(runner != null){   Thread moribund = runner;   runner = null;   moribund.interrupt();  } } public void run(){  while(Thread.currentThread() == runner){   //do stuff which can be interrupted if necessary  } } 

2) Your measurements list is accessed by multiple threads (the event thread and your user thread) at the same time without any synchronization. It looks like you don't have to roll your own synchronization, you can use aBlockingQueue.

3) You are creating a new Socket every iteration of your sending Thread. This is a rather heavyweight operation, and only really make sense if you expect measurements to be extremely infrequent (say one an hour or less). Either you want a persistent socket that is not recreated every loop of the thread, or you want a one shot runnable you can 'fire and forget' which creates a socket, sends all relevant data, and finishes. (A quick note about using a persistent Socket, socket methods which block, such as reading, cannot be interrupted by Thread.interrupt(), and so when you want to stop the thread, you must close the socket as well as calling interrupt)

4) There is little point in throwing your own exceptions from within a Thread unless you expect to catch it somewhere else. A better solution is to log the error and if it is irrecoverable, stop the thread. A thread can stop itself with code like (in the same context as above):

public void run(){   while(Thread.currentThread() == runner){    //do stuff which can be interrupted if necessary    if(/*fatal error*/){     stopThread();     return; //optional in this case since the loop will exit anyways    }   }  } 

Finally, if you want to be sure a thread exits with the rest of your application, no matter what, a good technique is to call Thread.setDaemon(true) after creation and before you start the thread. This flags the thread as a daemon thread, meaning the VM will ensure that it is automatically destroyed if there are no non-daemon threads running (such as if your app quits).

Obeying best practices with regards to Threads should ensure that your app doesn't hang or slow down the phone, though they can be quite complex :)

更多相关文章

  1. Android获取有线和无线(wifi)的IP地址
  2. android获取mac地址
  3. android 获取网络IP地址
  4. Android(安卓)GPS 获得 经纬度 并得到该 坐标 精确地址
  5. android获取mac地址方法
  6. Android(安卓)获取以太网Mac地址和IP地址
  7. android: 不需要焦点的TextView跑马灯 MarqueeTextView
  8. Android(安卓)获取当前设备的IP地址
  9. Android(安卓)获取当前设备的IP地址

随机推荐

  1. Android(安卓)解析Json
  2. Android(安卓)Studio之项目突然出现乱码
  3. Android(安卓)开发技术周报 Issue#290
  4. android 动态注册JNI函数过程源码分析
  5. Android(安卓)Studio如何导入第三方库进
  6. Android多媒体开发高级编程——为智能手
  7. 纯C语言INI文件解析
  8. Android(安卓)-- 经验分享
  9. Android(安卓)Studio: Plugin with id 'a
  10. Android培训班(70)Dex文件里类定义dvmDef