package com.netease.mobimail; import android.app.ActivityManager; import android.app.ActivityManager.RunningTaskInfo; import android.app.Application; import android.content.ComponentName; import android.content.Context; import com.netease.mobimail.activity.BaseActivity; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; public class WearMailApplication extends Application { public static BaseActivity msCurrentActivity; private static Map<String, BaseActivity> sActivityMap = new ConcurrentHashMap(); public static void addActivity(BaseActivity paramBaseActivity) { sActivityMap.put(paramBaseActivity.getClass().getName(), paramBaseActivity); } public static void clearAll() { try { Iterator localIterator = sActivityMap.entrySet().iterator(); while (localIterator.hasNext()) { BaseActivity localBaseActivity = (BaseActivity)((Map.Entry)localIterator.next()).getValue(); localIterator.remove(); if ((localBaseActivity != null) && (!localBaseActivity.isFinishing())) { localBaseActivity.finish(); } } } finally {} } public static BaseActivity getActivityByClass(Class<?> paramClass) { return (BaseActivity)sActivityMap.get(paramClass.getName()); } public static BaseActivity getCurrentActivity() { return msCurrentActivity; } public static boolean isAppInForeground(Context paramContext) { try { List localList = ((ActivityManager)paramContext.getSystemService("activity")).getRunningTasks(1); if (localList.isEmpty()) { return false; } boolean bool2 = ((ActivityManager.RunningTaskInfo)localList.get(0)).topActivity.getPackageName().equalsIgnoreCase(paramContext.getPackageName()); return bool2; } catch (Exception localException) { if (getCurrentActivity() == null) { break label73; } } if (!getCurrentActivity().isFinishing()) {} label73: for (boolean bool1 = true;; bool1 = false) { return bool1; } } public static void setCurrentActivity(BaseActivity paramBaseActivity) { msCurrentActivity = paramBaseActivity; } public void onCreate() { super.onCreate(); CrashHandler.getInstance().init(); } }
![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心。
在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从此Map也有安全的了。
ConcurrentHashMap具体是怎么实现线程安全的呢,肯定不可能是每个方法加synchronized,那样就变成了HashTable。
从ConcurrentHashMap代码中可以看出,它引入了一个“分段锁”的概念,具体可以理解为把一个大的Map拆分成N个小的HashTable,根据key.hashCode()来决定把key放到哪个HashTable中。
在ConcurrentHashMap中,就是把Map分成了N个Segment,put和get的时候,都是现根据key.hashCode()算出放到哪个Segment中:
测试程序:
- import java.util.concurrent.ConcurrentHashMap;
- public class ConcurrentHashMapTest {
- private static ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<Integer, Integer>();
- public static void main(String[] args) {
- new Thread("Thread1"){
- @Override
- public void run() {
- map.put(3, 33);
- }
- };
- new Thread("Thread2"){
- @Override
- public void run() {
- map.put(4, 44);
- }
- };
- new Thread("Thread3"){
- @Override
- public void run() {
- map.put(7, 77);
- }
- };
- System.out.println(map);
- }
- }
ConcurrentHashMap中默认是把segments初始化为长度为16的数组。
根据ConcurrentHashMap.segmentFor的算法,3、4对应的Segment都是segments[1],7对应的Segment是segments[12]。
(1)Thread1和Thread2先后进入Segment.put方法时,Thread1会首先获取到锁,可以进入,而Thread2则会阻塞在锁上:
(2)切换到Thread3,也走到Segment.put方法,因为7所存储的Segment和3、4不同,因此,不会阻塞在lock():
以上就是ConcurrentHashMap的工作机制,通过把整个Map分为N个Segment(类似HashTable),可以提供相同的线程安全,但是效率提升N倍,默认提升16倍。