开发者

Android Thread details

I can create a class which extends Thread and overrides the run method with a loop. I then start it with myThread.start(), which create the OS thread and executes my run(). That's all good.

However, I don't quite understand the details. I'll have a go at working this out using test code when I get the chance, but before then can anyone answer these:

Q1. When does the constructor get executed, presumably when myThread is declared, or on start()?

Q2. What happens when my run() code completes? Is there a way of getting it to run again in the same thread (i.e. not losing all the thread variable values defined in class) Presumably calling start() might create a new os thread?

Q3. Presumably just calling myThread.run() would execute my run() in t开发者_如何学Pythonhe context of the current activity, not mythread, in which case how could it access the thread variables?)

-Frink


A1) When you construct the instance of your MyThread class

A2) Threads cannot be run twice or restarted, as stated in the documentation.

A3) Yes, calling run() directly will execute that function in the current Thread, not in a new Thread. It doesn't make much sense to create a class that extends Thread if you want to just call run(). You should always call start().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜