
Activity启动模式?
standard:默认方式,每次新建一个Activity。
singleTop:栈顶复用,比如播放器和消息接收页面常使用SingleTop。
singleTask:栈内复用,一个栈内只存在一个实例,比如新闻详情页面,不管从哪个地方打开都只存在一个实例,在onNewIntent()回调中获取参数。
singleInstance:比较特殊,会启用一个新的栈结构,不要用于中间页面,否则会出现跳转问题。
Handler机制?MessageQueue,Looper?
关于MessageQueue和Looper:https://www.cnblogs.com/rh1910362960/p/12575440.html
如何保证一个线程只有一个Looper?
一个线程只能有一个Looper,当想要在当前线程创建Looper,需要使用Looper.prepare(),若已有Looper的线程会抛出RuntimeException(“Only one Looper may be created per thread”)异常,那Android是如何保证一个线程只有一个Looper的呢?看源码就知道了是通过ThreadLocal实现的,参见下方源码。
public class Looper {
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
...
}
那什么又是ThreadLocal呢?看JDK的注释:
Implements a thread-local storage, that is, a variable for which each thread has its
own value. All threads share the same ThreadLocal object, but each sees a different
value when accessing it, and changes made by one thread do not affect the other threads.
The implementation supports null values.
大致意思是ThreadLocal实现了线程本地存储,所有线程共享一个ThreadLocal对象,但不同线程仅能访问其线程相关联的值,一个线程修改ThreadLocal对象对其他线程没有影响。
ThreadLocal为编写多线程并发提供了新的思路,我们可以将ThreadLocal理解为一大存储块,将这一大存储块分割给每个线程一小块,每个线程就拥有属于自己的一块存储区,对自己存储区的操作就不会影响其他线程,对于ThreadLocal<Looper>,则每一小块就存储着对应线程的Looper。
HashMap底层原理?
整理笔记:https://juejin.cn/post/6844904103546454029
加密算法有哪些?对称加密和非对称加密的区别?
对称加密:加密双方使用相同的秘钥,如果一方遭泄漏,那么整个通信就会被破解。常见加密方式有DES、AES。
非对称加密:使用一对秘钥,一个用来加密一个用来解密,而且公钥是公开的,私钥自己保存,不需要向对称加密那样在通信前先同步秘钥。总体来说,非对称加密安全性更好。常见加密方式有RSA、背包算法。