在Threads之间传递对象时是否可以使用instanceof? [英] Is it possible to use instanceof when passing objects between Threads?

查看:106
本文介绍了在Threads之间传递对象时是否可以使用instanceof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过instanceof工作的问题,然后却没有。进入细节很困难,但我认为这可能是问题所在:

I've run into an issue where instanceof works, and then it doesn't. Going into details is difficult, but I think this might be the problem:

阅读本文: http://www.theserverside.com/news/thread.tss?thread_id=40229 (搜索Thread.currentThread),似乎意味着即使这两个对象是同一个类,如果你在具有不同类加载器的线程之间传递它们,instanceof(和isAssignableFrom)可能仍会失败。

Reading this: http://www.theserverside.com/news/thread.tss?thread_id=40229 (search for Thread.currentThread), it seems to imply that, even if the two objects are the same class, if you pass them between threads with different class loaders, instanceof (and isAssignableFrom) might still fail.

这当然会解释我的行为,但我想知道是否有人可以验证它?

This certainly would explain the behavior I'm having, but I was wondering if anyone could verify it?

(我希望在讨论开始时链接的文章仍然可用,但是它似乎不是。)

(I wish the article linked at the beginning of the discussion was still available, but it doesn't seem like it is.)

推荐答案

这与线程无关,只与类加载器有关。当由不同的类加载器加载时,相同的类定义被JVM视为两个不同的类。所以 instanceof 或两者之间的转换失败。

This has nothing to do with threads, only with class loaders. The same class definition, when loaded by different classloaders, is seen as two different classes by the JVM. So instanceof or casts between the two fail.

所以回答你原来的问题:在线程之间传递对象由相同的类加载器加载是安全的, instanceof 等。工作正常。

So to answer your original question: passing objects between threads loaded by the same class loader is safe and instanceof et al. works fine.

这是关于类加载问题的文章

另请参阅我之前的这个答案,用于验证哪些类加载器在游戏。

See also this earlier answer of mine for a way to verify which classloaders are in the game.

以下是测试<$ c $行为的一些代码c> instanceof ,等等:

URL[] urls = new URL[] {new File("build/classes/").toURL()};
ClassLoader loader1 = new URLClassLoader(urls, null);
ClassLoader loader2 = new URLClassLoader(urls, null);
Class<?> c1 = loader1.loadClass("net.torokpeter.Foo");
Class<?> c2 = loader2.loadClass("net.torokpeter.Foo");
Object foo1 = c1.newInstance();
Object foo2 = c2.newInstance();

System.out.println("c1.toString(): " + c1);
System.out.println("c2.toString(): " + c2);
System.out.println("c1.equals(c2): " + c1.equals(c2));
System.out.println("c1 == c2: " + (c1 == c2));
System.out.println("foo1: " + foo1);
System.out.println("foo2: " + foo2);
System.out.println("foo1 instanceof Foo: " + (foo1 instanceof Foo));
System.out.println("foo2 instanceof Foo: " + (foo2 instanceof Foo));
System.out.println("c1.isAssignableFrom(c1): " + c1.isAssignableFrom(c1));
System.out.println("c2.isAssignableFrom(c2): " + c2.isAssignableFrom(c2));
System.out.println("c1.isAssignableFrom(c2): " + c1.isAssignableFrom(c2));
System.out.println("c2.isAssignableFrom(c1): " + c2.isAssignableFrom(c1));
System.out.println("c1.isAssignableFrom(Foo.class): " + c1.isAssignableFrom(Foo.class));
System.out.println("c2.isAssignableFrom(Foo.class): " + c2.isAssignableFrom(Foo.class));
System.out.println("Foo.class.isAssignableFrom(c1): " + Foo.class.isAssignableFrom(c1));
System.out.println("Foo.class.isAssignableFrom(c2): " + Foo.class.isAssignableFrom(c2));

输出是(在Eclipse中,Java5):

And the output is (in Eclipse, Java5):

c1.toString(): class net.torokpeter.Foo
c2.toString(): class net.torokpeter.Foo
c1.equals(c2): false
c1 == c2: false
foo1: net.torokpeter.Foo@360be0
foo2: net.torokpeter.Foo@45a877
foo1 instanceof Foo: false
foo2 instanceof Foo: false
c1.isAssignableFrom(c1): true
c2.isAssignableFrom(c2): true
c1.isAssignableFrom(c2): false
c2.isAssignableFrom(c1): false
c1.isAssignableFrom(Foo.class): false
c2.isAssignableFrom(Foo.class): false
Foo.class.isAssignableFrom(c1): false
Foo.class.isAssignableFrom(c2): false

所以一切似乎都是一致的:-)

So everything seems to be consistent :-)

这篇关于在Threads之间传递对象时是否可以使用instanceof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆