Analysis of threads in Android Development

Analysis of threads in Android Development

After finishing the tasks given by the company this morning, I suddenly got a little bored. So I turned over the things I learned before, read the blog, and saw a blog about threads. There was a great controversy, and I almost misunderstood it (I feel tall ~ ~ ~). The overall code is almost like this:

What is the so-called controversy? After reading the general meaning of this code, many people will think that a new thread is opened for execution every 2S, but when the program exits, the thread is still executed. The blogger added removecallbacks() to ondestroy(). The general meaning is to remove the thread (destroy).

Of course, the first thing to point out is that this demo executes runable every 2 seconds. It is equivalent to executing the main thread every 2 seconds and printing it with logcat. Instead of opening a new thread every 2 seconds. Whether you add the last sentence or not, you will find that the thread has not stopped. It is equivalent to executing once every 2 seconds and endless loop. Why is it executed? Well, that's a problem. Well, I won't sell off.

When exiting, the program is just finished. If it is just a simple finish, the program is not destroyed. The main thread continues to run, but is overwritten by the previous activity.

Some people say: the Android application itself is a process. When we exit the application, the process has been destroyed, right?

But not

Android features: when the application exits, the process resources are not destroyed. This is to start the program faster next time... I think the idea of Android is to maximize resource utilization, but this also has disadvantages. If an application needs system resources and system resources are not enough, you have to destroy other resources to make room

If you want to know whether it has not been destroyed, you can judge whether the savedinstancestate is equal to null in oncreate to know whether it has been reinitialized, or call system. Exit (0) to exit the process in onbackpressed.

One of the characteristics of Android: the user does not destroy the activity. The life cycle of the activity is completely managed by the system, and the system will recycle resources at an appropriate time. When you press the backspace key, you simply pop up the next activity in the stack and press the original activity on the stack. The system saves some historical information of the original activity and does not destroy it. It can be restored quickly when you open it next time.

The second feature of Android: the process resources have not been destroyed and the main thread of the activity has not exited. Therefore, the message queue in the activity will execute consistently. When the activity is started again, an rrunnable will be instantiated and added to the message queue.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>