Skip to content

Latest commit

 

History

History
183 lines (154 loc) · 8.48 KB

File metadata and controls

183 lines (154 loc) · 8.48 KB

Android Process & Thread

  • http://developer.android.com/guide/components/processes-and-threads.html
  • 1 Process & 1 Thread when start application
  • You can set up process for activity, service, receiver, provider using android:process in manifest
  • application for default process value
  • Process Lifecycle & Importance
    • Foreground
    • Visible
    • Service
    • Background
    • Empty
  • Method that run in system callback always run in “UI Thread"
  • "UI Thread” block more that 5 second will cause ANR (Application Not Responding) dialog.
  • Android UI Toolkit is not thread safe
    • Do not block the UI Thread
    • Do not access Android UI Components outside the UI Thread
  • Accessing UI Thread from other thread
    • Activity.runOnUiThread(Runnable)
    • View.post(Runnable)
    • View.postDelayed(Runnable, long)
  • Using Thread can cause the code complicated, consider using Handler or AsyncTask

Android Activity Life Cycle

Varargs (Variable number of arguments)

Autoboxing

Final (JAVA)

  • http://en.wikipedia.org/wiki/Final_(Java)
  • Final Classes
    • Final class can not be subclassed
    • Grant security and efficiency benefits
    • Many of JAVA standard libraries are final
    • Method inside final class implicitly work like final method
  • Final Methods
    • Cannot be overridden or hidden by subclasses
    • Prevent unexpected behaviour of subclass
    • Final method has nothing to do with efficiency or performance issue
  • Final Variables

Annotation

Javadoc

Thread Safety

  • http://en.wikipedia.org/wiki/Thread_safety
  • A code is thread safe if it only manipulates shared data structure in a manner that guarantees safe execution by multiple thread at the same time
  • Thread Safe
    • Implementation is guaranteed to be free of race conditions when accessed by multiple thread simultaneously
  • Conditionally Safe
    • Different thread can access different objects simultaneously, and access of shared data is protected from race condition
  • Not Thread Safe
    • Code should not accessed simultaneously by different thread

Race Condition

  • http://en.wikipedia.org/wiki/Race_condition#Computing
  • A behaviour of electronic, software or other system where the output is dependent on the sequence or timing of other uncontrollable events
  • Two signal racing each other to influence the output first
  • Critical race condition often happens when processes or threads depend on the shared state

Dead Lock

Difference Between Atomic, Volatile, Synchronize

Java Synchronized Analysis

Atomic Variable

Android Background Process Tutorial

Synchronized Method

Thread Pooling

  • http://en.wikipedia.org/wiki/Thread_pool_pattern
  • Creating number of threads to perform number of tasks.
  • When a thread done a task, it request next task until all the tasks are done.
  • After all the tasks are done, thread can be either terminated or wait for new tasks.

Handler, Looper, Message, Queue

iOS strong, retain, atomic, non-atimic

Android SQLite Tutorial

Volatile

Volatile in Java 5

Volatile

The volatile keyword is used:

  • to make non atomic 64-bit operations atomic: long and double. (all other, primitive accesses are already guaranteed to be atomic!)
  • to make variable updates guaranteed to be seen by other threads + visibility effects: after writing to a volatile variable, all the variables that where visible before writing that variable become visible to another thread after reading the same volatile variable (happen-before ordering).

Transient

Native

Generic programming

Covariant return type

Android Testing

More to Read