80,351
社区成员
发帖
与我相关
我的任务
分享
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
...
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));
}
public void set(T value) {
Thread currentThread = Thread.currentThread(); //获取当前线程
Values values = values(currentThread); //读取线程成员变量localValues
if (values == null) {
values = initializeValues(currentThread); //初始化该线程localValues,并重新读取
}
values.put(this, value); //添加该Looper对象
}
public T get() {
// Optimized for the fast path.
Thread currentThread = Thread.currentThread(); //获取当前线程
Values values = values(currentThread); //读取线程成员变量localValues
if (values != null) {
Object[] table = values.table;
int index = hash & values.mask;
if (this.reference == table[index]) {
return (T) table[index + 1]; //如果之前已经执行prepare(),此处返回不为空,导致再次执行prepare()抛出异常
} else {
values = initializeValues(currentThread);
}
return (T) values.getAfterMiss(this);
}
这是修改过来的,之前的弄错了
public void set(T value) {
Thread currentThread = Thread.currentThread(); //获取当前线程
Values values = values(currentThread); //读取线程成员变量localValues
if (values == null) {
values = initializeValues(currentThread); //初始化该线程localValues,并重新读取
}
values.put(this, value); //添加该Looper对象
}
public T get() {
// Optimized for the fast path.
Thread currentThread = Thread.currentThread(); //获取当前线程
Values values = values(currentThread); //读取线程成员变量localValues
if (values != null) {
Object[] table = values.table;
int index = hash & values.mask;
if (this.reference == table[index]) {
return (T) table[index + 1];
}
} else {
values = initializeValues(currentThread);
}
return (T) values.getAfterMiss(this); //这句看意思应该是null 太长了,懒得看^_^
}
没人回复,只能靠自己啦,看看写的还能不能对别人有些帮助