Home » Android » multithreading – How to pause/resume thread in Android?

multithreading – How to pause/resume thread in Android?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a thread that running into an activity. I don’t want that the thread continuos running when the user click the home button or, for example, the user receive a call phone.
So I want pause the thread and resume it when the user re-opens the application.
I’ve tried with this:

protected void onPause() {
  synchronized (thread) {
    try {
      thread.wait();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  super.onPause();
}
protected void onResume() {
  thread.notify();
  super.onResume();
}

It stops the thread but don’t resume it, the thread seems freezed.

I’ve also tried with the deprecated method Thread.suspend() and Thread.resume(), but in this case into Activity.onPause() the thread doesn’t stop.

Anyone know the solution?

How to&Answers:

Use wait() and notifyAll() properly using a lock.

Sample code:

class YourRunnable implements Runnable {
    private Object mPauseLock;
    private boolean mPaused;
    private boolean mFinished;

    public YourRunnable() {
        mPauseLock = new Object();
        mPaused = false;
        mFinished = false;
    }

    public void run() {
        while (!mFinished) {
            // Do stuff.

            synchronized (mPauseLock) {
                while (mPaused) {
                    try {
                        mPauseLock.wait();
                    } catch (InterruptedException e) {
                    }
                }
            }
        }
    }

    /**
     * Call this on pause.
     */
    public void onPause() {
        synchronized (mPauseLock) {
            mPaused = true;
        }
    }

    /**
     * Call this on resume.
     */
    public void onResume() {
        synchronized (mPauseLock) {
            mPaused = false;
            mPauseLock.notifyAll();
        }
    }

}

Answer:

Try the below code it will work

Thread thread=null;

OnResume()

  public void onResume(){
  super.onResume();
  if(thread == null){
  thread = new Thread()
  {
      @Override
      public void run() {
          try {

              }
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  };

  thread.start();
      }
  }

onPause()

@Override
 public void onPause(){
super.onPause();
if(thread != null){
Thread moribund = thread;
thread = null;
moribund.interrupt();
}
}