如何强制AsyncTask的等待 [英] how to force the AsyncTask to wait

查看:278
本文介绍了如何强制AsyncTask的等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的AsyncTask等到它完成。所以我写了下面的code和我用获得()方法如下,并作为code如下所示

  mATDisableBT =新ATDisableBT();

但在运行时获得()犯规力ATDisableBT等待,监守在logcat中我收到ATDisableBT和ATEnableBT发布的消息,混合秩序
这意味着获得()上ATDisableBT没有强迫它等待

如何强制AsyncTask的等待

code

  // preparatory第1步
    如果(this.mBTAdapter.isEnabled()){
        mATDisableBT =新ATDisableBT();
        尝试{
            mATDisableBT.execute()获得()。
        }赶上(InterruptedException的E){
            e.printStackTrace();
        }赶上(为ExecutionException E){
            e.printStackTrace();
        }
    }    //使BT。
    this.mATEnableBT =新ATEnableBT();
    this.mATEnableBT.execute();


解决方案

您应该在UI线程中执行的AsyncTask,因此使用的get() - 这将阻止它没有任何意义 - 它可能让你ANR误差

如果您是在蜂窝起来,然后AsyncTasks是单执行人线程中执行,串行 - 所以你应该mATEnableBT后mATDisableBT得到执行。有关详细请看这里:

http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)

您也可以从AsyncTask的切换到执行人。 AsyncTask的使用执行者执行。通过创建单线程的执行者,你要确保任务将得到串行执行:

  ExecutorService的执行人= Executors.newSingleThreadExecutor();
// ...  executor.submit(新的Runnable(){
    @覆盖
    公共无效的run(){
      //你的mATDisableBT code
    }
  });
  executor.submit(新的Runnable(){
    @覆盖
    公共无效的run(){
      //你的mATEnableBT code
    }
  });

i want the AsyncTask to wait till it finishes. so i wrote the below code and i used .get() method as follows and as shown below in the code

    mATDisableBT = new ATDisableBT();

but at run time the .get() doesnt force ATDisableBT to wait, becuase in the logcat i receive mixed order of messages issued from ATDisableBT and ATEnableBT which means .get() on ATDisableBT did not force it to wait

how to force the AsyncTask to wait

code:

//preparatory step 1
    if (this.mBTAdapter.isEnabled()) {
        mATDisableBT = new ATDisableBT();
        try {
            mATDisableBT.execute().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    //enable BT.
    this.mATEnableBT = new ATEnableBT();
    this.mATEnableBT.execute();

解决方案

You should execute AsyncTask on UI thread, so using get() - which will block it makes no sense - it might get you ANR error.

If you are on HONEYCOMB and up, then AsyncTasks are executed on single executor thread, serially - so your mATEnableBT should get executed after mATDisableBT. For more see here:

http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)

You might also switch from AsyncTask to Executors. AsyncTask is implemented using executors. By creating single threaded executor you make sure tasks will get executed serially:

ExecutorService executor = Executors.newSingleThreadExecutor();
//...

  executor.submit(new Runnable() {
    @Override
    public void run() {
      // your mATDisableBT code
    }
  });
  executor.submit(new Runnable() {
    @Override
    public void run() {
      // your mATEnableBT code
    }
  }); 

这篇关于如何强制AsyncTask的等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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