What is the difference between Android multi-threaded communication and multi-process communication? How are they implemented separately?

Source: Internet
Author: User

  When a program starts for the first time, Android starts a Linux process and a main thread. By default, all components of the program will run in that process and thread. At the same time, Android assigns a single Linux user to each application. Android will try to keep a running process, only in the event of insufficient memory resources, Android will try to stop some processes to free up enough resources for other new processes to use, but also to ensure that the current process users are accessing has sufficient resources to respond to the user's events in a timely manner. Threads are an integral part of the process and are the basis of CPU scheduling. In general, there are main threads and other thread points, and only the main thread can refresh the UI. After the application starts, the Activitythread main thread is created.

Components of different package names can run in the same process in a certain way.

After an activity is started, there will be at least 3 threads. A main thread and 2 binder threads.

1. There are several ways to communicate between Android threads:

1) shared variables (memory)

2) Piping

3) Handle mechanism    

Runonuithread (Runnable)

View.post (Runnable)

The message-driven mechanism within the Android process---handler,messagequeue,runnable,looper

Looper and Message Handling mechanisms: First creates a handler object in the main thread, in order to handle messages sent from the Cheng, and then uses the message object when the child thread has the request to send the message, which is first stored In queue Message Queuing, the main thread also has a looper message poll that loops through messages in the message queue and sends a message to handler processing when a message is found (updating the UI, and so on). Handler call Handlemessage is processed and the message is NULL for recycling.

2 Inter-process communication

Inter-process communication:

Bind mechanism (IPC->AIDL)

Linux-level shared memory

Boradcast

Data can be passed through intent between activity.

3. Android End Process several ways

1) Use the Restartpackage (String packname) method in Activitymanager to configure permissions in the manifest file

2) android.os.process.killProcess (int pid) can only terminate the process of this program

3) System.exit ()

4) After the android2.2 version, you can no longer use the Restartpackage () method, but you should use the Killbackgroundprocesses () method, and you should configure the permissions

5) Use reflection to call Forcestoppackage to end
  1. Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
  2. forceStopPackage.setAccessible(true);
  3. forceStopPackage.invoke(am, yourpkgname);
you need to add a definition in the configuration file:android:shareduserid= "Android.uid.system"Additionally, you need to add permissions to the configuration file:<uses-permission android:name= "Android.permission.FORCE_STOP_PACKAGES" ></uses-permission>

6) using the Linux command kill-9

7) Exit to main screen

 Public boolean onKeyDown (int  keycode, KeyEvent event) {         //  Event.getrepeatcount (): Press the return key without repeating        if (keycode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) { c10/>New  Intent (intent.action_main);               Home.setflags (intent.flag_activity_clear_top);               Home.addcategory (intent.category_home);               StartActivity (home);        }                  return Super . OnKeyDown (KeyCode, event);       

What is the difference between Android multi-threaded communication and multi-process communication? How are they implemented separately?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.