How to pause and resume streams in Android?

I just noticed that the suspension and resumption in the android thread are out of date. What is the job for this or how can I pause and resume the thread in android?

+6
source share
1 answer

Indeed, pausing or stopping threads at random points is an unsafe idea, so these methods are deprecated.

The best thing you can do, in my opinion, is to have fixed pause points in your threading run method and stop there using wait :

 class ThreadTask implements Runnable { private volatile boolean paused; private final Object signal = new Object(); public void run() { // some code while(paused) { // pause point 1 synchronized(signal) signal.wait(); } // some other code while(paused) { // pause point 2 synchronized(signal) signal.wait(); } // ... } public void setPaused() { paused = true; } public void setUnpaused() { paused = false; synchronized(signal) signal.notify(); } } 
+7
source

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


All Articles