afunms

My Software,My Dream—Forge a more perfect NMS product.

difference between two classLoaders

struts里总用到Thread.currentThread().getContextClassLoader()
而我们一般只用到Class.forName()或Class.getClassLoader()

所以想知道二者有什么区别,以下是从网上找到的:


Classloader存在下面问题:
在一个JVM中可能存在多个ClassLoader,每个ClassLoader拥有自己的NameSpace。
一个ClassLoader只能拥有一个class对象类型的实例,但是不同的ClassLoader可能
拥有相同的class对象实例,这时可能产生致命的问题。如ClassLoaderA,装载了
类A的类型实例A1,而ClassLoaderB,也装载了类A的对象实例A2。逻辑上讲A1=
A2,但是由于A1和A2来自于不同的ClassLoader,它们实际上是完全不同的,
如果A中定义了一个静态变量c,则c在不同的ClassLoader中的值是不同的。

Class.getClassLoader() returns the ClassLoader that loaded the class it
is invoked on.

Thread.getContextClassLoader() returns the ClassLoader set as the
context ClassLoader for the Thread it is invoked on, which can be
different from the ClassLoader that loaded the Thread class itself if
the Thread's setContextClassLoader(ClassLoader) method has been invoked.
This can be used to allow the object starting a thread to specify a
ClassLoader that objects running in the thread should use, but the
cooperation of some of those objects is required for that to work.


 

posted on 2007-10-16 17:41 afunms 阅读(110) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

My Links

News

留言簿(18)

随笔档案

相册

搜索

最新评论

阅读排行榜