In Java, is it always true that if thread1.getId() == thread2.getId()
, then thread1.equals(thread2)
?
I'm trying to track down a bug in a multithreaded application, and I noticed that it compares threads using Thread.equals()
instead of comparing their IDs. The Thread
class doesn't override its equals
method, so the only way for two Thread
objects to be equal is if they have the same memory address.
Edit:
So far I see two reasons why the answer might be no.
thread1
might have been terminated, andthread2
might be reusing the same ID. Therefore the ids are equal but the threads are not.thread1
andthread2
are references to the same thread, but they're not the same object. (Not sure if this is possible.)