Lesson 4 Multi-process in Android

multi-Progress

The first case: between two applications, it is also the relationship between process and process. Not much discussed in this section.

The second case: open multiple processes in one application.

Open multi-process mode

Opening method: Assign the android:process attribute to the four major components (Activity, Service, Receiver, ContentProvider) in AndroidMenifest.

Example:

SecondActivity process name: com.syy.note:remote

ThirdActivity process name: com.syy.note.remote

<activity
    android:name="com.syy.note.MainActivity"
    android:configChanges="orientation|screenSize"
    android:label="@string/app_name"
    android:launchMode="standard"
    <intent-filter>  
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
/>

<activity
    android:name="com.syy.note.SecondActivity"
    android:configChanges="orientation|screenSize"
    android:label="@string/app_name"
    android:process=":remote"
/>

<activity
    android:name="com.syy.note.ThirdActivity"
    android:configChanges="orientation|screenSize"
    android:label="@string/app_name"
    android:process="com.syy.note.remote"
/>

ps: View process list command: adb shell ps

Multi-process mode operation mechanism

  • Static members and singleton mode are completely invalid: for example, when a static variable is defined in a class, when a process changes the value of the variable, when another process obtains the value of the static variable, the obtained value is still the initial value.
  • The thread synchronization mechanism is completely invalid: different processes are not using the same piece of memory, and neither the lock object nor the lock global class can guarantee thread synchronization, because different processes lock the same object.
  • The reliability of SharedPreferences decreases: the bottom layer of sp is realized by reading/writing XML files, and concurrent writing will cause problems
  • Application will be created multiple times: separate virtual machines will be allocated to different processes, which is equivalent to starting a separate application, so a new Application will also be created

 

 

 

 

 

Guess you like

Origin blog.csdn.net/wishxiaozhu/article/details/114646320