publicHandler(){ if (FIND_POTENTIAL_LEAKS) { final Class<? extends Handler> klass = getClass(); if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && (klass.getModifiers() & Modifier.STATIC) == 0) { Log.w(TAG, "The following Handler class should be static or leaks might occur: " + klass.getCanonicalName()); } } mLooper = Looper.myLooper(); if (mLooper == null) { thrownew RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = null; }
publicstaticfinalvoidprepare(){ if (sThreadLocal.get() != null) { thrownew RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper()); }
publicbooleansendMessageAtTime(Message msg, long uptimeMillis) { boolean sent = false; MessageQueue queue = mQueue; if (queue != null) { msg.target = this; sent = queue.enqueueMessage(msg, uptimeMillis); } else { RuntimeException e = new RuntimeException( this + " sendMessageAtTime() called with no mQueue"); Log.w("Looper", e.getMessage(), e); } return sent; }
@OverridevoidaddNewEntry(K key, V value, int hash, int index){ LinkedEntry<K, V> header = this.header;
// Remove eldest entry if instructed to do so. LinkedEntry<K, V> eldest = header.nxt; if (eldest != header && removeEldestEntry(eldest)) { remove(eldest.key); }
// Create new entry, link it on to list, and put it into table LinkedEntry<K, V> oldTail = header.prv; LinkedEntry<K, V> newTail = new LinkedEntry<K,V>( key, value, hash, table[index], header, oldTail); table[index] = oldTail.nxt = header.prv = newTail; }
publicfinal V get(K key){ if (key == null) { thrownew NullPointerException("key == null"); }
V mapValue; synchronized (this) { //获取到值时,就返回该值 mapValue = map.get(key); if (mapValue != null) { hitCount++; return mapValue; } missCount++; }
/* * Attempt to create a value. This may take a long time, and the map * may be different when create() returns. If a conflicting value was * added to the map while create() was working, we leave that value in * the map and release the created value. */
//尝试创建一个值,这个方法的默认实现是直接返回null。但是在它的设计中,这个方法可能执行完成之后map已经有了变化。 V createdValue = create(key); if (createdValue == null) { returnnull; // 如果不为没有命名的key创建新值,则直接返回 null }
if (mapValue != null) { //如果不为空,说明不需要我们所创建的值,所以又把返回的值放进去 // There was a conflict so undo that last put map.put(key, mapValue); } else { //为空,说明我们更新了这个key的值,需要重新计算大小 size += safeSizeOf(key, createdValue); } }
publicMyBitmapUtils(){ mMemoryCacheUtils = new MemoryCacheUtils(); mLocalCacheUtils = new LocalCacheUtils(); mNetCacheUtils = new NetCacheUtils(mLocalCacheUtils, mMemoryCacheUtils); }