详细分析Android中实现Zygote的源码

2020-01-06 13:36:52丽君

有求必应之等待请求——runSelectLoop

zygote从startSystemServer返回后,将进入第四个关键的函数:runSelectLoop。我们来看一下这个函数的实现:

 

 
  1. static final int GC_LOOP_COUNT = 10;  private static void runSelectLoop() throws MethodAndArgsCaller { 
  2. ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();  ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>(); 
  3. FileDescriptor[] fdArray = new FileDescriptor[4];   
  4. fds.add(sServerSocket.getFileDescriptor());  peers.add(null); 
  5.   int loopCount = GC_LOOP_COUNT; 
  6. while (true) {  int index; 
  7. if (loopCount <= 0) {  gc(); 
  8. loopCount = GC_LOOP_COUNT;  } else { 
  9. loopCount --;  } 
  10.   try { 
  11. fdArray = fds.toArray(fdArray);  index = selectReadable(fdArray); 
  12. } catch(IOException ex) {  throw new RuntimeException("Error in select()", ex); 
  13. }   
  14. if (index < 0) {  throw new RuntimeException("Error in select()"); 
  15. } else if (index == 0) {  ZygoteConnection newPeer = acceptCommandPeer(); 
  16. peers.add(newPeer);  } 
  17. }  }