Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 858359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:32:03+00:00 2026-05-15T08:32:03+00:00

I’ve run into an issue where instanceof works, and then it doesn’t. Going into

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:32:03+00:00Added an answer on May 15, 2026 at 8:32 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.