为什么不是线程停止? [英] Why isn't the thread stopping?

查看:135
本文介绍了为什么不是线程停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务产生一个新的线程,并根据的通常推荐的Java 中断的方法()'ING。当我停止服务,我停止的onDestroy线程()。服务停止,并且中断code为达到。不过,很快从可运行的开始线程将重新启动。

 公共类DoScan延伸服务{
公共挥发性主题亚军;

@覆盖
公众的IBinder onBind(意向意图){
返回null;
}

@覆盖
公共无效的onCreate(){
super.onCreate();

startThread();
}

@覆盖
公共无效的onDestroy(){
super.onDestroy();
android.util.Log.v(@@@@@@@@@@@@@@@@@@@@,DoScan.onDestroy);
stopThread();
}


市民同步无效startThread(){
如果(亚军== NULL){
android.util.Log.v(@@@@@@@@@@@@@@@@@@@@,DoScan.startthread);
亚军=新主题(新ScanningThread());
runner.start();
}
}
/ *通过大部分的OnCreate使用一个循环的循环处理程序。
*在scanningthread做的工作,然后通知SVC的uithread
* /

市民同步无效stopThread(){
如果(亚军!= NULL){
android.util.Log.v(@@@@@@@@@@@@@@@@@@@@,DoScan.stopthread);
螺纹垂死=亚军;
亚军= NULL;
moribund.interrupt();
android.util.Log.v(@@@@@@@@@@@@@@@@@@@@,打断?+ moribund.isInterrupted());
}
}
        }
 

解决方案

我觉得最保险的办法是有一个标志,所以线程检查它里面的主循环。

 类ScanningThread继承Thread {
//必须是挥发性:
私人挥发性布尔停止= FALSE;

公共无效的run(){
而(!停止){
的System.out.println(活着);
}
如果(停止)
的System.out.println(检测站);
}

市民同步无效requestStop(){
停止= TRUE;
}
}

市民同步无效startThread(){
如果(亚军== NULL){
android.util.Log.v(@@@@@@@@@@@@@@@@@@@@,DoScan.startthread);
亚军=新ScanningThread();
runner.start();
}
}

市民同步无效stopThread(){
如果(亚军!= NULL){
android.util.Log.v(@@@@@@@@@@@@@@@@@@@@,DoScan.stopthread);
runner.requestStop();
亚军= NULL;
}
}
 

My service spawns a new thread, and stops it according to the typically recommended java method of interrupt()'ing. When I stop the service, I stop the thread in onDestroy(). The service is stopped, and the interrupt code is reached. However, soon enough the thread restarts from the beginning of the Runnable.

public class DoScan extends Service {
	public volatile Thread runner;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		startThread();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "DoScan.onDestroy");
		stopThread();
	}


	public synchronized void startThread(){
		if(runner == null){
			android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "DoScan.startthread");	  
			runner = new Thread(new ScanningThread());
			runner.start();
		}
	}
	/* use a handler in a loop cycling through most of oncreate.
	 * the scanningthread does the work, then notifies the svc's uithread
	 */

	public synchronized void stopThread(){
		if(runner != null){
			android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "DoScan.stopthread");
			Thread moribund = runner;
			runner = null;
			moribund.interrupt();
			android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "interrupted?" + moribund.isInterrupted());
		}
	}
        }

解决方案

I think the safest way is to have a flag so the thread checks for it inside its main loop.

class ScanningThread extends Thread {
	// Must be volatile:
	private volatile boolean stop = false;

	public void run() {
		while (!stop) {
			System.out.println("alive");
		}
		if (stop)
			System.out.println("Detected stop");
	}

	public synchronized void requestStop() {
		stop = true;
	}
}

public synchronized void startThread(){
	if(runner == null){
		android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "DoScan.startthread");         
		runner = new ScanningThread();
		runner.start();
	}
}

public synchronized void stopThread(){
	if(runner != null){
		android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "DoScan.stopthread");
		runner.requestStop();
		runner = null;
	}
}

这篇关于为什么不是线程停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆