Android 問題(一)

List各種

List

Just an interface.
當需要synchronized時候,使用List,如LiveData<List< Object >>

ArrayList

  • extends AbstractList
  • implements List, RandomAccess, Cloneable, Serializable
  • 不支持synchronized

Map各種

Map vs. List

如果已知position:map 和 list get() 操作爲O(1).

LinkedHashMap

Hash table and linked list implementation of the Map interface

  • extends HashMap<K,V>
  • implements Map<K,V>

HashMap

  • extends AbstractMap<K,V>
  • implements Map<K,V>, Cloneable, Serializable
  • allow null
  • HashMap is non synchronized
  • thread Not Safe
  • Fast
  • isn’t legacy

HashTable

  • extends Dictionary<K,V>
  • implements Map<K,V>, Cloneable, Serializable
  • not allow null
  • Hashtable is synchronized.
  • thread Safe
  • Slow
  • It’s Legacy(old fashion)

Map

just an interface.

List & View

ExpandableList

RecycleView

Variable keyword

abstract

  • abstract class vs. abstract method
  • 如果是inner class 要用一個interface (WordDao), 如在abstract的“Room database” class, 也要把這個interface 的constructor 寫成abstract。 public abstract WordDao wordDao();

volatile (易揮發)

The Java volatile keyword is used to mark a Java variable as “being stored in main memory”. Use a synchronized in that case to guarantee that the reading and writing of the variable is atomic.

Reading and writing of volatile variables causes the variable to be read or written to main memory. Reading from and writing to main memory is more expensive than accessing the CPU cache. Accessing volatile variables also prevent instruction reordering which is a normal performance enhancement technique.

only use volatile variables when you really need to enforce visibility of variables.

多線程

https://www.youtube.com/playlist?list=PLZ9NgFYEMxp50tvT8806xllaCbd31DpDy

AsyncTask

encapsulates the creation of a background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

Thread

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

The Java Virtual Machine continues to execute threads until either of the following occurs:
(1) The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
(2) All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

Handler

allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue.

There are two main uses for a Handler:
(1) to schedule messages and runnables to be executed at some point in the future;
(2) to enqueue an action to be performed on a different thread than your own.

Timer

A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

  • thread safe
  • does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.

Implementation note: This class scales to large numbers of concurrently scheduled tasks (thousands should present no problem). Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks.

Android ViewModel

Subclasses must have a constructor which accepts Application as the only parameter.
來自MVVM 官方 Framework
因爲有生命週期,常與LiveData一起用
Activity 與 Fragment之間數據傳輸是友好的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章