当前位置: 代码迷 >> J2SE >> 关于setclassLoader,该怎么处理
  详细解决方案

关于setclassLoader,该怎么处理

热度:70   发布时间:2016-04-23 19:35:40.0
关于setclassLoader
class A{
static Map cacheMap;
public static void doSmthing(){
Cache cache = new Cache();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
//为何要set一下classloader?
Thread.currentThread().setContextClassLoader(A.class.getClassLoader());

Integer cacheId = RandomUtils.nextInt();

cacheMap.put(cacheId,cache);
} finally{
Thread.currentThread().setContextClassLoader(cl);
}
}
}
//这样做的目的是什么?是不是和线程有关?

------解决思路----------------------
单纯看这些代码,不太有必要的样子
毕竟设完之后就执行了两句就又设回去了


如果不设classloader,看看执行效果上会有什么区别吗?
  相关解决方案