AsyncTaskScheduler icon indicating copy to clipboard operation
AsyncTaskScheduler copied to clipboard

Bug: cancelAllTasks doesn't cancel all tasks in the correct way

Open AndroidDeveloperLB opened this issue 7 years ago • 0 comments

I've noticed that if I call cancelAllTasks , it still lets some tasks to start. Tasks that had a long time till they should be started. Here's the code to test it:

    Executor defaultPoolExecutor = Executors.newSingleThreadExecutor();
    final AsyncTaskScheduler asyncTaskScheduler = new AsyncTaskScheduler(defaultPoolExecutor);
    for (int i = 0; i < 1000; ++i) {
        final int finalI = i;
        asyncTaskScheduler.execute(new SingleAsyncTask<Void, Void>() {
            @Override
            protected Void doInBackground() {
                Log.d("AppLog", "doInBackground:" + finalI );
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Log.d("AppLog", "interrupted");
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onExecuteSucceed(final Void o) {
                super.onExecuteSucceed(o);
            }
        });
    }
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            asyncTaskScheduler.cancelAllTasks(true);
            asyncTaskScheduler.execute(new SingleAsyncTask<Void, Void>() {
                @Override
                protected Void doInBackground() {
                    Log.d("AppLog", "doInBackground:NEW " );
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Log.d("AppLog", "interrupted");
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onExecuteSucceed(final Void o) {
                    super.onExecuteSucceed(o);
                }
            });

        }
    }, 1500);

You will notice that the log shows something like that:

05-31 16:20:07.890 25294-25333/com.example.user.myapplication D/AppLog: doInBackground:0 05-31 16:20:08.894 25294-25333/com.example.user.myapplication D/AppLog: doInBackground:1 05-31 16:20:09.511 25294-25333/com.example.user.myapplication D/AppLog: interrupted 05-31 16:20:09.548 25294-25333/com.example.user.myapplication D/AppLog: doInBackground:541 05-31 16:20:09.549 25294-25333/com.example.user.myapplication D/AppLog: interrupted 05-31 16:20:09.558 25294-25333/com.example.user.myapplication D/AppLog: doInBackground:704 05-31 16:20:09.558 25294-25333/com.example.user.myapplication D/AppLog: interrupted 05-31 16:20:09.565 25294-25333/com.example.user.myapplication D/AppLog: doInBackground:883 05-31 16:20:09.565 25294-25333/com.example.user.myapplication D/AppLog: interrupted 05-31 16:20:09.569 25294-25333/com.example.user.myapplication D/AppLog: doInBackground:NEW

This is not a good thing, because the tasks run on a single thread, so it shouldn't be possible to see multiple "interrupted" , and surely not of so new tasks (jumped from 1 to 541...).

AndroidDeveloperLB avatar May 31 '17 13:05 AndroidDeveloperLB