How to use Runnable.wait () in AsyncTask? Why is AsyncTask not waiting ...?

I am using AsyncTask to start a background operation. Of course, switching to another thread, although it is already running in the background thread, does not make much sense at all, except that the other thread is a user interface thread. This is what I would like: during the execution of the task, I need to “access” the user interface, for example. to show a dialog box to ask the user how to proceed.

  • complete background task
  • stop the task at some point to get user feedback.
  • switch to UI thread to display a dialog box and request input
  • Go back to the background task and continue.

How can I do that? I thought I could use Runnablewith myActivity.runOnUiThread(runnable), but this does not work:

private void copyFiles() {
    CopyTask copyTask = new CopyTask(this);
    copyTask.execute();
}

// CustomAsyncTask is a AsyncTask subclass that takes a reference to the current
// activity as parameter
private class CopyTask extends CustomAsyncTask<Void, Void, Void> {
    private doCopy;

    @Override
    protected Boolean doInBackground(Void... params) {              
        // Custom code, e.g. copy files from A to B and check for conflict
        for (File file : allFiles) {
            doCopy = true;

            if (isConflict(file)) {
                // Stop current thread and ask for user feedback on UI Thread

                Runnable uiRunnable = new Runnable() {
                    public void run() {
                        // Pos 1. --> Execute custom code, e.g. use AlertDialog to ask user if file should be replaced...
                        doCopy = false;

                        synchronized (this) {
                           this.notify();
                        }
                    }
                });  

                synchronized(uiRunnable) {
                    // Execute code on UI thread
                    activity.runOnUiThread(uiRunnable);

                    // Wait until runnable finished
                    try {
                        uiRunnable.wait();
                    }
                    catch (InterruptedException e ) {
                        e.printStackTrace();
                    }
                }
            }

            // Pos 2. --> Continue work
            if (doCopy)
               copyFromAToB(File);
        }

        return null;
    }
}

Inside doInBackground()(-> in the background thread) AsyncTaskcalls activity.runOnUiThread(uiRunnable). The following is called uiRunnable.wait(). Regarding document wait() , you need to do the following:

Causes the calling thread to wait until another thread calls notify () or notifyAll () of this object.

Thus, the background thread must wait for its work to continue until this.notify()(== uiRunnable.notifiy()) is called into another thread (= UI thread), right?

, id ! uiRunnable.wait() , if (doCopy).... , (, , ...) , , , doCopy = false if (doCopy) .

? wait() , ? - ?

!

EDIT: : , AsyncTask, , , , (. ).

AsyncTask, , AsyncTask , . , //.

wait(), . , AsyncTask, wait() , .

+4
1

AsyncTask onPreExecute() . , doInBackground().

doInBackground() onPostExecute() , . doInBackground(), onProgressUpdate(), , doInBackground(), .

- :

private class DoStuffTask extends AsyncTask {

   @Override
   protected void doInBackground(Object... args) {
       // Do stuff
       onProgressUpdate(x);
       // Do more stuff
   }

   @Override
   protected void onProgressUpdate(Object... args) {
       // Update your UI here
   }
}

, , , AsyncTask doInBackground(), , AsyncTasks. AsyncTask, , AsyncTask, .

, AlertDialog , , , , AsyncTask AlertDialog.

wait() AsyncTask

AsyncTask, wait AsyncTask, , , - . Runnable , doInBackground() , AsycTask.

;

public class TestTask extends AsyncTask{

    private boolean notified;
    private Promptable p;
    public interface Promptable { public abstract void prompt(); }

    public TestTask(Promptable p){
        this.p = p;
    }

    @Override
    protected Object doInBackground(Object... arg0) {
        Log.d("First", "First");
        onProgressUpdate(null);
        synchronized(this){
            while(!notified){
                try{
                    this.wait();
                }
                catch(InterruptedException e){ }                    
            }
        }
        Log.d("Second", "Second");
        return null;
    }

    @Override 
    protected void onProgressUpdate(Object... args){       
        synchronized(this){
            notified = true;
            p.prompt();               
            this.notify();
        }
    }
}

, Activity AsyncTask Promptable. , wait() while. , - notify() wait(), . , , , while , , .

, .

+7

Source: https://habr.com/ru/post/1546964/


All Articles