In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to implement the Android constructor". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
To put it simply, a pipe is a file, and at both ends of the pipe, there are two open file descriptors, both of which correspond to the same file, one for reading and the other for writing. the general way of use is that a thread reads the contents of the pipe by reading the file descriptor, and when the pipe has no content, the thread enters a waiting state. Another thread writes to the pipe by writing a file descriptor, and when writing content, if there is a thread waiting for the contents of the pipe on the other side, the thread will be awakened.
How does this wait and wake-up operation work, with the help of the epoll mechanism in the Linux system.
The epoll mechanism in Linux system has been improved to handle a large number of handles. Poll is an enhanced version of the multiplexed IO interface select/poll under Linux. It can significantly reduce the CPU utilization of the system when there is only a small amount of active programs in a large number of concurrent connections.
But the only IO interface we need to monitor here is mWakeReadPipeFd, that is, the reader of the pipe we created earlier, so why do we need to use epoll? It's kind of like killing a chicken with a cow knife.
In fact, this Looper class is very powerful. In addition to monitoring the internal pipe interface, it also provides an addFd interface for external interface calls. Through this interface, outsiders can add the IO events they want to monitor to the Looper object. When events occur on all these monitored IO interfaces, they will wake up the corresponding threads to deal with them. But here we are only concerned with the occurrence of the IO event of the pipe we just created.
Let's go back to the constructor of NativeMessageQueue and look at the process of creating the Looper object in the JNI layer, that is, how its constructor is implemented, which is implemented in the frameworks/base/libs/utils/Looper.cpp file:
[cpp] view plaincopyLooper::Looper (bool allowNonCallbacks): mAllowNonCallbacks (allowNonCallbacks), mResponseIndex (0) {int wakeFds [2]; int result = pipe (wakeFds); MWakeReadPipeFd = wakeFds [0]; mWakeWritePipeFd = wakeFds [1]; # ifdef LOOPER_USES_EPOLL / / Allocate the epoll instance and register the wake pipe. MEpollFd = epoll_create (EPOLL_SIZE_HINT);. Struct epoll_event eventItem; memset (& eventItem, 0, sizeof (epoll_event)); / / zero out unused members data field union eventItem.events = EPOLLIN; eventItem.data.fd = mWakeReadPipeFd; result = epoll_ctl (mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & entItem); # else. # endif. }
What this constructor does is very important. it is closely related to the two knowledge points that the main thread of the application enters a waiting state when there is no message in the message queue and wakes up the main thread of the application when there is a message in the message queue. It mainly creates a pipe through pipe system calls:
[cpp] view plaincopyint wakeFds [2]
Int result = pipe (wakeFds)
.
MWakeReadPipeFd = wakeFds [0]
MWakeWritePipeFd = wakeFds [1]
To use the epoll mechanism of the Linux system, first create a file descriptor specific to epoll through epoll_create:
[cpp] view plaincopymEpollFd = epoll_create (EPOLL_SIZE_HINT)
The parameter EPOLL_SIZE_HINT passed in is the number of * * file descriptors that can be monitored on this mEpollFd.
That's all for "how to implement the Android constructor". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.