扬中市新闻网站/seo软件优化工具软件
在android程序中一定不能阻塞UI线程,否则很容易就会弹出no respond 的对话框,导致程序退出。为了避免这种情况,一般需要较长时间执行的任务都放在线程中去执行,如从网上下载图片在界面的某个区域显示。但是使用线程回导致cpu开销过大,并且当线程管理不好时也会弹出no respond框。android提供了AsyncTask类用来处理这种情况;例如需要下载图片,然后在songImagButton中显示,下载的逻辑在songImageButton的initImage方法中
private class DrawImageTask extends AsyncTask<Void, Integer, Void>
{// 更新图片显示区域,显示图片@Overrideprotected void onPostExecute(Void params){// TODO Auto-generated method stubsongImageButton.invalidate();this.cancel(false);}// 下载图片@Overrideprotected Void doInBackground(Void... params){// TODO Auto-generated method stubsongImageButton.initImage();return null;}}
一般在doInBackground中执行后台的逻辑,如下载图片或其他需要耗时长的任务,onPostExecute方法用来对界面进行操作。如果后台的逻辑有返回值,则由doInBackground返回然后传入onPostExecute方法,然后更新界面。doInBackground方法和onPostExecute的参数必须对应,这两个参数在AsyncTask声明的泛型参数列表中指定,第一个为doInBackground接受的参数,第二个为显示进度的参数,第三个为doInBackground返回和onPostExecute传入的参数。
当然我个人认为AsyncTask不能完全取代线程,在一些逻辑较为复杂或者需要在后台反复执行的逻辑就可能需要线程来实现了。
怎样停止AsyncTask和Thread
在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异常,但是如果捕获该异常的代码后面还有其他代码,则这些代码还会继续执行。
具体示例代码如下:
public class AsyncTaskTest extends Activity
{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// set the six buttons listenerButton 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();}});}private class TestAsyncTask extends AsyncTask<String, Integer, Double>{double a;public TestAsyncTask(double a){this.a = a;}@Overrideprotected 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 blocke.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* */private class ThreadForTestSleep extends Thread{public void run(){Log.i("ThreadForTestSleep", "sleep start");try{sleep(30000);} catch (InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();}double a;for (int i = 0; i < 10000000; i++){a = i * i + i;Log.d("-----", "a:" + a);}Log.i("ThreadForTestSleep", "sleep end");}}/*** test wait* */private class ThreadForTestWait extends Thread{public void run(){Log.i("ThreadForTestWait", "wait start");try{synchronized (this){wait();}} catch (InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();}Log.i("ThreadForTestWait", "wait end");}}
}
网上有人测试过,同种类型的 AsyncTask 实例,最大数量为 20 个。
如果超过,则会引发 RejectExcuteException,并 crash
因此,要考虑到用户的操作是否会引发频繁的 AsyncTask 实例化,若存在,则从 UI 上进行改进,比如:
分页机制;
线程池;
生产者-消费者模型。
2.取消AsyncTask任务
文档已经说明,cancel()方法不一定能成功,所以 onCancel() 回调方法不一定被调用。
但若一个 AsyncTask 结束,则一定会调用 onPostExcute() 方法。
对于一个 doInBackground()方法中含有 while/for 循环时,最好配置 isCancel标置来 break 循环。
这里有一个其他的例子,也挺好的,简单易懂。