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之间数据传输是友好的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值