views:

211

answers:

2

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:

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.)

Thanks, Peter

+13  A: 

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.

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

Here is an article about class loading issues.

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

Update to Romain's comment

Here is some code to test the behaviour of instanceof, among others:

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));

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 :-)

Péter Török
-1 Even in different classloader aClass instanceof aClassName will still resolve to true. If you are comparing them by == then it will not work.
Romain Hippeau
@Romain, I strongly doubt that, as it would mean that `instanceof` returns true, then class cast fails with exception! Which would be completely illogical and make `instanceof` totally unreliable. Don't have concrete evidence ready though - if you do, feel free to present it.
Péter Török
@Peter I removed the -1. The objects are seen as different types by the JVM, but instanceof will still return true. You are right in that the classes will throw a cast exception.
Romain Hippeau
So far, as near as I can tell, Péter is correct. instanceof, isAssignableFrom() and class cast *all* fail. I'm going to output the classloaders and see what's happening.
Risser
@Romain, I added some test code and its output which shows that `instanceof` returns `false` indeed.
Péter Török
Yeah, using your code that shows the classloader, when my program works, the two classloaders are the same, and when it doesn't work, there are two different classloaders being used. Thanks!
Risser
@Péter I bow to your answer.
Romain Hippeau
Two class loaders may share a parent class loader, which may load some classes. These are the same in both class loaders, maybe this is the source of the confusion.
starblue
@starblue, correct. The classloaders above are created with `new URLClassLoader(urls, null)` which sets their parent to `null`. However, if they are created as `new URLClassLoader(urls);`, they share the same parent and then the two `Foo` classes loaded by them are one and the same.
Péter Török
A: 

The problem is as Péter Török says, with classloaders. Incidentially, this is also the reason for JNDI which allows common objects to be created by a single, central classloader (meaning also that the classes you need, need to be in the classpath of the single central classloader, giving all kinds of fun when you need more than just Strings).

Thorbjørn Ravn Andersen