2

I'm using the Looper attached to an Android HandlerThread to perform long-running asynchronous tasks:

HandlerThread handlerThread = new HandlerThread("handler-thread");
handlerThread.start()
Handler myHandler = new Handler(callbacks, handlerThread.getLooper());
// Ready to handle messages in callbacks

When I'm done with the worker thread, I call handlerThread.getLooper().quit() to stop its Looper. Does the HandlerThread instance terminate (exit, die) after its Looper is quit(), assuming that that thread is performing no other execution?

mjama
  • 2,650
  • 2
  • 22
  • 24
  • see http://stackoverflow.com/questions/8907626/is-this-a-prefect-way-to-stop-handlerthread/8907981#8907981 – Juan Rada Jan 30 '14 at 03:10

2 Answers2

2

Does the HandlerThread instance terminate (exit, die) after its Looper is quit(), assuming that that thread is performing no other execution?

After the Looper quits, the worker thread's run() method returns if there is no other processing to be performed in run(). At this point, handlerThread.isAlive() returns false and handlerThread.getState() returns TERMINATED.

Note that it is never legal to start a thread more than once, as noted in the Thread API doc.

Lorne Laliberte
  • 6,261
  • 2
  • 35
  • 36
mjama
  • 2,650
  • 2
  • 22
  • 24
0

it's been mentioned in another answer that a Thread in Java is never garbage-collected.

No it hasn't. Your link doesn't mention any such thing.

A Thread is eligible for garbage-collection like any other object, when it is unreachable. In the case of a thread that can't happen before it exits. That is what your link says.

Your question is founded on a misconception.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @downvoter Please explain. Either the link says that or it doesn't. It doesn't; and if it did it would be wrong as stated. – user207421 Feb 03 '14 at 20:02
  • I guess the downvoter thinks you're talking about things unrelated to the question. Indeed, the current question has nothing to with the original one. – maaartinus Feb 04 '14 at 01:37