I just noticed that suspend and resume in android threading has been deprecated. What is the work around for this or how can I suspend and resume a thread in android?
How to Suspend and Resume Threads in android?
Asked Answered
how about service: developer.android.com/guide/topics/fundamentals/services.html –
Background
Hi LeoLink I dont think services would sort my problem. thanks anywyays :) –
Tridentum
Indeed, suspending or stopping threads at random points is an unsafe idea, which is why these methods are deprecated.
The best you can do in my opinion is to have fixed points of pausing in your thread's run
method and stopping 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();
}
}
Oh ok I understand it now @Tudor. btw another clarification is this similar to signalling in threads? –
Tridentum
@rosesr: It is similar to condition variables in linux for example. –
Respective
© 2022 - 2024 — McMap. All rights reserved.