开发者

Dealing with threads

What is the proper way to manage threads working in the background?

For example, I have Activity that creates several threads. I need to do following:

1) Destroy all threads when Application is destroyed

2) Pause threads created within Activity if user navigate away from Activity

3) Destroy threads created within Activity if Activity is destroyed

The only thing that come to my mind would be to have all threads variables declared as public the to be able to issue t.destroy() or something similar on these events that I listed above. First, I am not sure if this is right way a开发者_如何学Got all, and secondly, I don't like it because I will have to change code to make sure I can reference all threads I created.

For example, I have situation where my Activity instantiate new object (ex. LoadImages.class) and that objects creates several thread depending on how many images is to be loaded. The threads are not visible from the calling activity.

So, do I have to pass threads references to calling activity, or there is some way to know who is the parent of the thread and destroy only thread with particular parent Activity?


For LoadingImages I think there is a simple solution: have a public method on LoadingImage called release that will allow it to release its own resources.

If each of your activities is destroying its own threads, I don't see the need for your step 1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜