Android concurrent programming: (1) Basic knowledge-Architecture and components, android Architecture

Source: Internet
Author: User
Tags java se

Android concurrent programming: (1) Basic knowledge-Architecture and components, android Architecture

All content in this section is original. If you need to reprint it, please indicate the source.

Http://blog.csdn.net/manoel/article/details/38462631

Preface

I haven't written a blog for a long time. I am afraid of mistakes due to my limited level, I feel that I have nothing to write, and I am looking for a balance between work, study, and life.

I have been researching and sorting out Android multi-thread programming recently, hoping to share this with you.

I think everyone who has done Android Application Development should know that multithreading is a very strange place. It is called "weird" because the bugs produced by multithreading are very difficult to reproduce and are irregular.

What makes programmers feel terrible than "unregular.

If you are not familiar with Android development, refer to the Android development tutorial I translated earlier.

Http://blog.csdn.net/column/details/development4android.html


Recently, I will update some principles and skills about Android multithreading from time to time, all of which are my own summary of some practices, hoping to be further written.

Now, go to the topic. To understand the multi-thread mechanism of Android, let's start with the entire Android architecture.

Software Stack
Applications
Application Layer, including programs written using the Java library and Android framework.
Core Java
The Java class library is not a full implementation of Java SE or Java ME, but an implementation of Apache Harmony, based on Java 5. This layer provides the basic Java Thread mechanism, including the java. lang. Thread class and java. util. concurrent package.
Application framework
The Android framework layer is mainly responsible for processing window systems, UI components, and resources. Basically, all classes required for writing an Android application are at this layer. At the same time, the Android Framework defines and manages the lifecycle of Android components and the communication between them. In addition, the Android Framework defines a series of Android-specific asynchronous mechanisms, including HandlerThread, AsyncTask, IntentService, AsyncQueryHandler, and Loaders.
Native libraries
C/C ++ library layer, mainly responsible for processing images, audio and video, databases, fonts, OpenGL, etc. Java programs do not directly communicate with this layer, because the Android framework has encapsulated these local code.
Runtime
The runtime environment, that is, the Dalvik virtual machine. A sandbox is provided for every Android application. The Dalvik virtual machine executes the. dex file. Each program runs on its proprietary Dalvik virtual machine.
Linux kernel
Linux Kernel, which is a potential operating system. Allows the program to use hardware functions, such as sound, network, and camera. It also manages processes and threads. Start a process for each program, and each process is in charge of a Dalvik virtual machine. The code of the multi-threaded execution program in the process. The Linux kernel allocates available CPU execution time for processes and their threads through the scheduling mechanism.
Program architecture

A program is based on the Application object and Android components: Activity, Service, BroadcastReceiver, and ContentProvicer.

Program

The expression of a program in Java is the android. app. Application object. When the program is enabled, this object is created and destroyed when the program is stopped. That is to say, the Application object goes through the full life cycle of a Linux Process. When the Linux Process is restarted, a new Application object is created.

Components

Android components include Activity, BroadcastReceiver, Service, and ContentProvider.

These entities have different responsibilities and lifecycles, but they all represent the entry of the program, that is, the place where the program starts.

One component can wake up another component through Intent. This process can occur in the same program or between multiple programs. Intent is divided into explicit and implicit:

Explicit Intent
Define the complete name of a component, which can be recognized by the program at runtime.
Implicit Intent
Use IntentFilter to bind a component at runtime. You need to define some "protocols" in IntentFilter ". If Intent matches the "protocol" in IntentFilter, this component will be started.

The lifecycle of components is a specific technology of the Android system. They do not match the potential Java objects. A Java object can survive longer than its components, and the Dalvik virtual machine can save multiple objects corresponding to a component. Be careful, this will cause memory leakage. I will analyze the memory leakage caused by multithreading later.

Generally, the way to implement a component is to subclass it. In addition, all components of a program must be registered in AndroidManifest. xml.

Activity

An Activity is a screen, which usually occupies all the screen sizes of the device. It is used to display information and process user input. Including all UI components, such as buttons, text boxes, and images.

Activity holds an object reference to all view trees, so it occupies a large amount of memory.

When you navigate between screens, the Activity instance is initialized from a stack. When you navigate to a new screen, a new Activity is pushed into the stack. When it is returned to the previous screen, the Activity is popped up from the stack.

In the following figure, an Activity A is enabled first, then switched to B, and A is destroyed at the same time, and then switched to C and D. A, B, and C are displayed in full screen mode, but D is in window mode and only occupies part of the screen space. A is completely destroyed, B is invisible, C is partially visible, D is completely visible, and D is on the top of the stack. Therefore, D obtains the focus and can receive user input. The position of the Activity in the stack determines the Activity status:

  • Visible and active: D
  • Paused and partially visible: C
  • Stop and completely invisible: B
  • Not interactive and completely destroyed:

The top Activity in a program has a certain impact on the priority of the program in the system. Here the so-called priority is the priority of the process, which will affect the probability of a program being terminated and the execution time allocated to the thread in the program.

You can press the return key or call the finish () method to terminate the lifecycle of an Activity.

Service

The Service is executed in the background. It is invisible and does not interact with users. It can be used to detach operations from other components, which are longer than those components. A Service can be executed in two ways: start or bind:

Started Service
Enable the service by calling Context. startService (Intent). The Intent here can be both implicit and explicit. To stop the service, call Context. stopService (Intent ).
Bound Service
By calling Context. bindService (Intent, ServiceConnection, int), multiple components can be bound to the same Service. The Intent here can be both implicit and explicit. After binding, you can use the ServiceConnection interface to communicate with the Service. To stop the service, call Context. unbindService (ServiceConnection ). After the last component is unbound from the Service, the Service will be destroyed.
ContentProvider

A program wants to share a large amount of data, whether in this program or between multiple programs, you can use ContentProvider.

It provides an entry to obtain data, which is usually suitable for SQLite databases. SQLite databases are private to programs, but data can be shared between processes through ContentProvider.

BroadcastReceiver

It listens to Intent from a program, between programs, or from the system. All in all, it listens to all intents on the phone, filters the received intents, and determines which one needs to be processed.

A BroadcastReceiver should be dynamically registered, that is, it can be controlled by registering where it is needed.

If it is statically registered in AndroidManifest. xml, when this program is installed, it is listening for Intent all the time, regardless of whether the Intent is useful for it.

In this way, if an Intent matches an IntentFilter, The BroadcastReceiver starts the program associated with it.


Let's take A closer look here. Assume there is program A, and A BroadcastReceiver listening for network changes is registered in AndroidManisest. xml.

At this time, A is not opened, that is, there is no process of A in the system.

When the network of the system changes, because the BroadcastReceiver exists, the process of A is created, and the Application Object of A is also created!

BroadcastReceiver is considered to be the entry to a program, which also includes Activity, Service, and ContentProvider.


Concurrent Programming

In general, concurrency is the most common case where many people click your website within a short period of time and send requests for processing.
Concurrency refers to sending, not Processing
Concurrent Programming is a response to the concurrency situation. It doesn't mean that you can't cope with it if you only have one processor. This solution is related to the architecture and algorithm. The CPU is generally time-based and will be switched to different threads continuously in a very short period of time. No matter how many concurrent threads are processed, it is only a matter of time.
How to improve the processing efficiency depends on the technology used.

Error Message

Related Article

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.