Android multithreading implements repeated start and stop services

Android multithreading implements repeated start and stop services

In order to avoid deadlock in multithreaded environment, open call is generally advocated. Open call can avoid deadlock at the cost of losing atomicity. But sometimes it seems logical,

For example:

There is no problem with this code in a single threaded environment. However, strange phenomena occur in multithreaded environment.

In principle, OnStart must be ahead of onstop, but this is not the case:

The steps are as follows:

Thread 1 start

Thread 2 stop

Thread 1

Thread 2

Thread 2 onstop

Thread 1 OnStart

Now onstop is ahead of OnStart. So the question is, how can we ensure that OnStart must be in front of onstop?

Then what is the problem? Why is the stop method not allowed to be invoked before start? If a class cannot be restarted, stop can be before start, otherwise it cannot be before start.

The above is the explanation of the repeated start and stop service of Android multithreading. If you have any questions, please leave a message or go to the community of this site for communication and discussion. Thank you for reading. I hope it can 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
分享
二维码
< <上一篇
下一篇>>