android如何停止Thread和AsyncTask
2012-08-10 17:15
447 查看
转自: http://www.android-study.com/jichuzhishi/376.html
我们知道在java的线程中,没有办法停止一个正在运行中的线程,在Android的AsyncTask中也是一样的。如果必须要停止一个线程,我们可以采用在这个线程中设置一个标志位,然后在线程run方法或AsyncTask的doInBackground方法中的关键步骤判断这个标志位以决定是否继续执行。然后在需要终止此线程的地方改变这个标志位以达到停止线程的目的。
从外部调用AsyncTask的cancel方法并不能停止一个已经启动的AsyncTask,这个cancel方法的作用与线程的interrupt方法相似,调用了一个线程的interrupt方法之后线程仍然运行,但是如果该线程的run方法里面调用过sleep的或者wait方法后,处于sleep或wait状态,则sleep和wait立即结束并且抛出InterruptedException异常。AsyncTask的cancel方法也一样,如果在这个Task的doInBackground方法中调用了sleep或wait方法,当在UI线程中调用了这个Task实例的cancel方法之后,sleep或wait立即结束并且抛出InterruptedException异常,但是如果捕获该异常的代码后面还有其他代码,则这些代码还会继续执行。测试代码如下:
我们来看看这个例子怎么样,这里主要用到了view.View.OnClickListener;监听,android.widget.Button按钮,我们定义一个Button是开始,一个Button定义为停止。这样我们就可以给按钮加上一个监听,在监听里定义当点击按钮时,就可以停止AsyncTask和Thread,这个方法我个人感觉非常的好。这个主要是加了一个sleep(30000);这样的话,我们就有时间来判断一下是否应该怎么样做。
我们知道在java的线程中,没有办法停止一个正在运行中的线程,在Android的AsyncTask中也是一样的。如果必须要停止一个线程,我们可以采用在这个线程中设置一个标志位,然后在线程run方法或AsyncTask的doInBackground方法中的关键步骤判断这个标志位以决定是否继续执行。然后在需要终止此线程的地方改变这个标志位以达到停止线程的目的。
从外部调用AsyncTask的cancel方法并不能停止一个已经启动的AsyncTask,这个cancel方法的作用与线程的interrupt方法相似,调用了一个线程的interrupt方法之后线程仍然运行,但是如果该线程的run方法里面调用过sleep的或者wait方法后,处于sleep或wait状态,则sleep和wait立即结束并且抛出InterruptedException异常。AsyncTask的cancel方法也一样,如果在这个Task的doInBackground方法中调用了sleep或wait方法,当在UI线程中调用了这个Task实例的cancel方法之后,sleep或wait立即结束并且抛出InterruptedException异常,但是如果捕获该异常的代码后面还有其他代码,则这些代码还会继续执行。测试代码如下:
package eoe.task; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class AsyncTaskTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // set the six buttons listener Button startButton = (Button) this.findViewById(R.id.StartTask); final TestAsyncTask task = new TestAsyncTask(0); startButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { task.execute("str1", "str2"); } }); Button endButton = (Button) this.findViewById(R.id.StopTask); endButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { task.cancel(false); } }); Button startSleepButton = (Button) this .findViewById(R.id.StartThread_sleep); final ThreadForTestSleep threadForTestSleep = new ThreadForTestSleep(); startSleepButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { threadForTestSleep.start(); } }); Button endSleepButton = (Button) this .findViewById(R.id.StopThread_sleep); endSleepButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { threadForTestSleep.interrupt(); } }); Button startWaitButton = (Button) this .findViewById(R.id.StartThread_wait); final ThreadForTestWait threadForTestWait = new ThreadForTestWait(); startWaitButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { threadForTestWait.start(); } }); Button endWaitButton = (Button) this.findViewById(R.id.StopThread_wait); endWaitButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { threadForTestWait.interrupt(); } }); } /** * AsyncTask * * @author alex * */ private class TestAsyncTask extends AsyncTask<String, Integer, Double> { double a; public TestAsyncTask(double a) { this.a = a; } @Override protected Double doInBackground(String... params) { for (String param : params) { Log.i("TestAsyncTask", "param:" + param); } Log.i("TestAsyncTask", "doInBackground is start"); for (int i = 0; i < 10000000; i++) { a = i * i + i; Log.d("-----", "a:" + a); } Log.i("TestAsyncTask", "sleep 1 is end"); try { Thread.sleep(30000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("TestAsyncTask", "sleep 2 is end and continue execute"); return a; } protected void onPostExecute(Double result) { Log.i("last a value is", "" + result); } } /** * test sleep * * @author Administrator * */ private class ThreadForTestSleep extends Thread { public void run() { Log.i("ThreadForTestWait", "sleep start"); try { sleep(30000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } double a; for (int i = 0; i < 10000000; i++) { a = i * i + i; Log.d("-----", "a:" + a); } Log.i("ThreadForTestWait", "sleep end"); } } /** * test wait * * @author Administrator * */ private class ThreadForTestWait extends Thread { public void run() { Log.i("ThreadForTestWait", "wait start"); try { synchronized (this) { wait(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("ThreadForTestWait", "wait end"); } } }
我们来看看这个例子怎么样,这里主要用到了view.View.OnClickListener;监听,android.widget.Button按钮,我们定义一个Button是开始,一个Button定义为停止。这样我们就可以给按钮加上一个监听,在监听里定义当点击按钮时,就可以停止AsyncTask和Thread,这个方法我个人感觉非常的好。这个主要是加了一个sleep(30000);这样的话,我们就有时间来判断一下是否应该怎么样做。
相关文章推荐
- Android 停止AsyncTask和Thread
- 安卓(Android)中如何优雅的 创建/执行 异步任务/回调(AsyncTask 、Thread、Job、CallBack、Handler) ?线程池(Executor)
- Android开发中立即停止AsyncTask和Thread的一些办法
- Android笔记(三十七) 如何停止AsyncTask?
- Android开发中立即停止AsyncTask和Thread的一些办法
- Android怎样停止AsyncTask和Thread
- Android如何让Handler中内部run停止(runnable/thread)
- Android开发中立即停止AsyncTask和Thread
- android多线程 : asynctask,thread,handler
- Android中R文件的丢失问题以及aapt.exe停止工作如何解决
- android View如何停止动画
- Android中如何解决Thread is already started问题
- android中Thread对于Adapter出现的报错情况的处理以及如何正确使用Spinner与SimpleCurosorAdapter
- Android 应用开发 之通过AsyncTask与ThreadPool(线程池)两种方式异步加载大量数据的分析与对比
- 如何停止Android Monkey脚本
- Android AsyncTask cannot resolve CalledFromWrongThreadException问题解决方案
- Android中的Thread与AsyncTask的区别
- Android Thread和AsyncTask的区别和联系
- Android MultiThread & Handler & AsyncTask
- Android中的Thread与AsyncTask的区别