1

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.

artist
  • 11
  • 3
  • try this http://stackoverflow.com/questions/8505707/android-best-and-safe-way-to-stop-thread – Riddhi Shah Jul 11 '14 at 07:37
  • maybe on `onDestroy()` in your MainActivity ?! – dazito Jul 11 '14 at 07:37
  • @RiddhiShah thanks, i'll see it – artist Jul 11 '14 at 07:56
  • @dwnz Thanks! if there is no other way. I should do that in "onDestory()". – artist Jul 11 '14 at 07:57
  • Um.. I found this link.(sorry for editing. I'm writing with a mobile phone) (http://androidcookbook.com/Recipe.seam;jsessionid=9ACF6480A5FD9DDFEDE7ED6CD35C97EF?recipeId=1370) From above link. "Thread.setDaemon(true)" is one of the alternative way. By setting thread as daemon thread, your thread will be stopped when main thread is terminated. – artist Jul 11 '14 at 11:05

2 Answers2

0

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
}
}
DCoder
  • 3,486
  • 7
  • 36
  • 68
  • Thanks! But my explain is not enough for you. In my case, thread in Singleton instance. Not in specific activity... – artist Jul 11 '14 at 08:02
0

@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".)

artist
  • 11
  • 3