My service spawns a new thread, and stops it according to the typically recommended java method of interrupt()'ing. When I stop the service, I stop the thread in onDestroy(). The service is stopped, and the interrupt code is reached. However, soon enough the thread restarts from the beginning of the Runnable.
public class DoScan extends Service {
public volatile Thread runner;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
startThread();
}
#Override
public void onDestroy() {
super.onDestroy();
android.util.Log.v("####################", "DoScan.onDestroy");
stopThread();
}
public synchronized void startThread(){
if(runner == null){
android.util.Log.v("####################", "DoScan.startthread");
runner = new Thread(new ScanningThread());
runner.start();
}
}
/* use a handler in a loop cycling through most of oncreate.
* the scanningthread does the work, then notifies the svc's uithread
*/
public synchronized void stopThread(){
if(runner != null){
android.util.Log.v("####################", "DoScan.stopthread");
Thread moribund = runner;
runner = null;
moribund.interrupt();
android.util.Log.v("####################", "interrupted?" + moribund.isInterrupted());
}
}
}
I think the safest way is to have a flag so the thread checks for it inside its main loop.
class ScanningThread extends Thread {
// Must be volatile:
private volatile boolean stop = false;
public void run() {
while (!stop) {
System.out.println("alive");
}
if (stop)
System.out.println("Detected stop");
}
public synchronized void requestStop() {
stop = true;
}
}
public synchronized void startThread(){
if(runner == null){
android.util.Log.v("####################", "DoScan.startthread");
runner = new ScanningThread();
runner.start();
}
}
public synchronized void stopThread(){
if(runner != null){
android.util.Log.v("####################", "DoScan.stopthread");
runner.requestStop();
runner = null;
}
}
The problem is that your thread needs to cooperate by periodically checking for interruption and exiting if the thread has been interrupted. Unless you place something along the lines of the following in your thread...
// Processing...
if ( Thread.interrupted() ){
return;
}
// More processing...
try{
Thread.sleep(sleeptime);
}catch(InterruptedException interrupt){
return;
}
// Rinse and repeat...
...your thread will just ignore the fact that it has been interrupted. The method proposed by Lucas S. is essentially identical, except that using interruption will generate an exception if the thread is blocked, whereas under Lucas S.'s method, you might have to wait indefinitely for the thread to quit.
Interrupting a thread throws an exception in the thread, it does not necessarily stop it. You should catch that exception and then do the clean up in thread before exiting (provided, you need to exit!).
Related
I have a question.
Recently I develop simple "Logging system" for Android.
There is one singleton class which name is "Logger".
protected Logger(){
....
_logHandler = new LogHandler(_logQueue);
_logHandler.start();
....
}
public static Logger getInstance(){
...
}
In "Logger", one thread is running just like below.
#Override
public void run() {
try{
while (isAlive){
execute();
synchronized (lock) {
try {
while (isPaused) {
lock.wait();
}
}catch (InterruptedException e){
e.printStackTrace();
}finally {
shutDown();
}
}
}
}catch(InterruptedException e){
e.printStackTrace();
}finally {
shutDown();
}
}
public void requestShutDown(){
isAlive = false;
interrupt();
}
What i want is when application is terminated, I would like to call "requestShutDown()" method to stop thread above.
But i can't find proper moment.
So, Do I have to
When onPause() method executed in Activity, call requestShutDown(). And onResume() method executed in Activity, call thread.start() again?
Is there another way?
Or When Application is terminated, all the resources in application(include thread, Logger class in above) are garbage collected properly?
thanks in advance.
you can create thread like :
Thread th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
th = new Thread(new Runnable() {
#Override
public void run() {
//do your stuff
}
});
th.run() //to start thread
}
public void requestShutDown(){
if(th.isAlive())
{
th.yield(); //to close thread
}
}
#dwnz Thank you!!
Finally, I call "onDestory()" method in MainActivity. In onDestory(), if isFinishing() of Activity is true, it will be terminated(Of course, this is not "necessary and sufficient condition".)
I want to make stopwatch. And i create stopwatch class like this. And when i call onPause in another Activity its freeze application.
public class StopWatch implements Runnable {
private Object mPauseLock;
private boolean mPaused;
private boolean mFinished;
private ArrayList<TextView> textFields;
private Handler mHandler = new Handler();
public StopWatch( ArrayList<TextView> textFields) {
mPauseLock = new Object();
mPaused = false;
mFinished = false;
this.textFields =textFields;
}
public void run() {
textFields.get(1).setText("progressing...");
if (!mPaused) {
mHandler.postDelayed(this, 1000);
}
synchronized (mPauseLock) {
while (mPaused) {
try {
mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}
}
public void onPause() {
synchronized (mPauseLock) {
mPaused = true;
}
}
public void onResume() {
synchronized (mPauseLock) {
mPaused = false;
mPauseLock.notifyAll();
}
}
}
and i create instance of class in another View like. Can somebody exmplain me where is problem?
stopky = new StopWatch(textFields);
stopky.run();
// do another stuff and register buttons with onClickListener and call
stopky.onPause(); // freeze application
stopky.onResume();
You can't Object.wait() in a run method called from a Handler, which is probably running on the main/UI Thread.
The whole Android app is coordinated via short methods which register with the main/UI Thread. You're probably registering your stopwatch there, too. It's not possible to perform a while loop there and at the same time process events from the user interface..
A quick solution would be to re-schedule your run method and check the status the next time it gets called. Basically like so:
public void run() {
textFields.get(1).setText("progressing...");
if (!mPaused) {
// do what has to be done when stopwatch is running
mHandler.postDelayed(this, 1000);
} else {
// just re-schedule with a shorter delay
mHandler.postDelayed(this, 10);
}
}
An even better way would be to go for a fully event-driven design and avoid calling the stopwatch at all while it is stopped. In this case, you would simply re-start it from the Button's event handler.
Is it possible to resume an interrupted Thread in Android?
You shouldn't resume Thread by its API, resume() method is depracated (reason).
You can simulate resuming Thread by killing it and starting a new one:
/**
Since Thread can't be paused we have to simulate pausing.
We will create and start a new thread instead.
*/
public class ThreadManager
{
private static GameThread gameThread = new GameThread();
public static void setRunning(boolean isRunning)
{
if (isRunning)
{
gameThread = new GameThread();
gameThread.setRunning(true);
gameThread.start();
}
else
{
gameThread.setRunning(false);
}
}
public static boolean isRunning()
{
return gameThread.isRunning();
}
public static void join() throws InterruptedException
{
gameThread.join();
}
}
Thread does not support these actions as related methods are deprecated.
suspend()
resume()
I need a timer which will send a message for its' handler. I've made a class that implements Runnable and I feed its' object to the thread runnable constructor. When I start the thread it hangs application and it obviously isn't working asyncroniously. I could've used AsyncTask but I've also heard that they're designed for short-term operations while my background timer must work throughout activity onResumed state. Would you mind pointing out my mistake and maybe giving useful links on the subject of threads in android. Thanks.
Here's the code I've written:
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
_myTimerInstance = new MyTimer(new Handler() {
#Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
// ...
}
});
_myThread = new Thread(_myTimerInstance);
_myThread.run();
}
private static class MyTimer implements Runnable {
private Handler _myHandler;
private boolean _activityHasBeenLeft;
public MyTimer(Handler myHandler) {
_myHandler = myHandler;
}
public void setActivityHasBeenLeft(boolean b) {
_activityHasBeenLeft = b;
}
#Override
public void run() {
while (!_activityHasBeenLeft) {
try {
Thread.sleep(2000);
_myHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
You should always use Thread.start() not Thread.run()
Thread.run() is like a normal method call and is run on the same thread.
use
_myThread.start();
I am writing a game in which after a specified amount of time a thread must be stopped.The user has failed to complete a particular level.I am using a thread.How do i stop this thread after a specified amount of time and display another view.How do i do this.The following code delays the launching of the thread by timelimit.
Thread t = ... t.join(timelimit);
if (t.isAlive)
t.interrupt();
How do i run the thread and close it after a period of time.
Your working thread
public class Worker extends Thread {
private boolean isRunning = true;
public void run() {
while (isRunning) {
/* do your stuff here*/
}
}
public void stopWorker() {
isRunning = false;
}
}
Your stopping thread
public class Stopper extends Thread {
private Worker worker;
public void Stopper(Worker w) {
worker = w;
}
public void run() {
// wait until your timeout expires
worker.stopWorker();
}
}
you should declare your thread with something like this
public class GameLoopThread extends Thread{
private boolean running = false;
public void setRunning(boolean run){
running = run;
}
#Override
public void run(){
while(running){
}
}
}
This is the safer way, In order to stop you should set the running variable to false. Otherwise If you stop the thead you will get an android exception .
I prefer interrupting the Thread from outside and checking interrupted state in short intervals:
try {
while (!Thread.currentThread.interrupted()) {
doSth();
}
} catch (InterruptedException e) {
// finished
}
you can use below code is run after given specified time.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// after this is rung
}
}, 5000);