简体   繁体   English

但是第一个Asynctask有效,按下按钮后第二个无效

[英]First Asynctask works however, Second doesn't when button pressed

I have seen all the answers to the previous questions however, can't understand the code to my needs and a bit confused. 我已经看到了前面问题的所有答案,但是无法理解我所需要的代码,并且有些困惑。

I am trying to run two AsyncTasks simultaneously which are place in two different classes when pressed button in my third class. 我试图同时运行两个AsyncTasks ,在我的第三堂课中按下按钮时,它们分别位于两个不同的课中。 I understand I need to used executeOnExecutor but what I can't understand where and what should be the contents to my needs of it. 我知道我需要使用executeOnExecutor但是我不知道我需要的内容在哪里以及应该包含哪些内容。 Do I use in both the classes when I'm implementing individually or in the third class where I'm executing when button is pressed. 我是在单独实现时还是在两个类中都使用时,还是在按下按钮时正在执行中的第三类中使用?

Code of third class where I'm executing AsyncTask: 我正在执行AsyncTask的第三类代码:

     new NewAppliance().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
     new NewChecksAndOperations().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

In both class where these are implemented, I'm targeting HONEYCOMB too as: 在实现这些的两个类中,我也将HONEYCOMB定位为:

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
@Override
public void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 11) 
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

In fact, you are already doing it right. 实际上,您已经做对了。

executeOnExecutor () works on a specific AsyncTask instance, hence has to be called for each and every AsyncTask instance for which you want it to take effect. executeOnExecutor ()在特定的AsyncTask实例上工作,因此必须为您要对其生效的每个AsyncTask实例调用。

That is, you can either call: 也就是说,您可以调用:

new MyAsyncTask(..).execute();  // <----  standard

For standard execution, or: 对于标准执行,或:

new MyAsyncTask(..).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

For parallel execution. 用于并行执行。

As I'm sure you already know, default execute() behavior for Android >= 3.0 is sequential processing. 如您所知,Android> = 3.0的默认execute()行为是顺序处理。 Remeber to both instantiate and execute() (or executeOnExecutor()) your AsyncTasks from the UI thread, 请记住从UI线程实例化和execute()(或executeOnExecutor())您的AsyncTask,


Finally, and I understand this is not the subject of your questions, please make a habit of wrapping all of your StrictMode processing within a DEBUG test: 最后,我知道这不是您要提出的问题,请养成将所有StrictMode处理包装在DEBUG测试中的习惯:

boolean isDebuggable = (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));

if (isDebuggable ) {
       set StrictMode here..
}

br> You do not want StrictMode tests running at your users... br>您不希望StrictMode测试在您的用户上运行...


EDIT 编辑


To test a fixed pool executor (instead of an AsyncTask) do as follows: 要测试固定池执行程序(而不是AsyncTask),请执行以下操作:

ExecutorService pool = Executors.newFixedThreadPool(2);

for (int i = 0; i < 2; i++) {
     pool.execute(new Runnable() {
           public void run() { 
               // do something with no UI access!
        }
      });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM