Android ' s Dalvik java vs. Java SEJava code optimizationMemory Management and allocationMulti-threaded operation of AndroidAndroid's Dalvik Java is compared with Java SE:
@, Dalvik virtual machine is register-based machine;java se vm is stack machine.
@, introduced the JIT (Just in time) compiler from the Android 2.2 version, before the pure interpreter.
@, the Android SDK uses the DX tool to convert the Java SE stack machine byte-code to Dalvik register-based machine Byte-code. (IDE automatically completes this step)
@, Dalvik VMs can run multiple instances on the same VM (running in multiple instances on the same machine). When the system starts, it opens the first instance, called Zygote, which controls all other instances.
Java code optimization:
There are many classes in the @, Java.util.concurrent package that have implemented concurrency control, so before writing the code, verify that there are classes that meet the requirements instead of customizing a class directly to control concurrency.
The control class (Reentrantreadwritelock) for read and write locks is provided in the @, Java.util.concurrent.locks package without the need to use synchronized to ensure thread safety.
Memory Management and allocation:
@, do not create new objects in the loop, which can cause excessive overhead to the GC.
@, using static factory method to achieve on-demand objects. can refer to the book "Effective Java", or can refer to the Android source code in the message, motionevent, parcel and other classes of implementation.
Multi-threaded operation of Android:
@Thread:
1, directly inherit the thread class.
2, realize runnable interface. The interaction with the main thread is realized through the Runonuithread method of activity.
3. Because the Start method can only be called once for a thread instance, it is necessary to create the thread object each time to complete the operation. This leads to excessive overhead, so this method is not recommended.
@, Asyntask:
1, OnPreExecute, Doinbackground, Onprogressupdate, OnPostExecute.
2, OnXxx are in the main thread execution, Doinbackground is not.
3. Call the Publishprogress method in Doinbackground to trigger the Onprogressupdate event.
4. The actual thread generated by this method is managed by Executorservice in the background.
5, this method is easy to achieve in a separate thread to complete long-time operation, without affecting the main thread.
6, this way each operation needs to create a new object, so it is not suitable for frequent calls to the scene, otherwise increase the cost of GC. Suitable for types of file downloads such infrequent operations.
@, Handler:
1, acitvity implementation of Handler.callback interface, in the Handlemessage method to complete the required operation, you can use the method into the reference message of the What field as the Switch-case statement flag, to complete different operations. Note: Add Message.recycle () at the end of the method; This allows you to reuse the message instance instead of repeatedly creating too many message instances.
2. Create the Handlerthread object (Handlerthread).
3. Create Handler object (Mhandler = new Handler (Handlerthread.getlooper (), this)).
Note: 2, 3 steps can be implemented in OnCreate.
4, the thread has the Looper class for maintenance, loop execution, until the manual execution terminates (can call Mhandle.getlooper () in the OnPause event. Quit ()).
5, need to interact with the main thread, you can add a Handler object (Mmainhandler = new Handler (Getlooper (), this)) is responsible for the interaction with the main thread.
6, through the handler of various sendxxx methods to complete the message delivery, ultimately by the Handlemessage method to complete the operation of the message.
Android programming:pushing the Limits--Chapter 2:efficient Java Code for Android