How to compare stream objects

I recently came across AWT sources EventQueuewhere I saw this piece of code:

final boolean isDispatchThreadImpl() {
    EventQueue eq = this;
    pushPopLock.lock();
    try {
        EventQueue next = eq.nextQueue;
        while (next != null) {
            eq = next;
            next = eq.nextQueue;
        }
        if (eq.fwDispatcher != null) {
            return eq.fwDispatcher.isDispatchThread();
        }
        return (Thread.currentThread() == eq.dispatchThread);
    } finally {
        pushPopLock.unlock();
    }
}

What really struggles with me is that stream objects are compared using ==. So far I have been doing this with help equals(Object). I have already addressed this question , but the two answers are actually not what I am looking for.

Is it possible that two different instances refer to the same native thread? How should I compare stream objects for equality?

+4
source share
3 answers

Is it possible that two different instances refer to the same native thread?

No.

According to Threadjavadoc:

- .

Thread :

  • start() Thread , . ; .. start() . ( .)

  • start() , run() , Thread . ( .) ​​

  • run() Thread , . ( , , .)

Thread ; .

?

== - .

equals(Object) , Thread Object, , ==.


.

, equals. Thread javadoc ( ) , equals == . , , Thread. , " ", . ( , "" ... , .)

, . , == .

+6

: , , .

, : , . , " " . , , "" () , .

, equals(Object) , , , . , , ""; , , , 99% , / .

, TJ Crowder: Thread.equals(Object) Object.equals(Object), ... . , . , Thread .

, : Java- .

+2

JSL-17:

, ,

, , . , .

equals(Object) . Thread == equals(Object).

Thread.equals(Object), java.lang.Object:

public boolean equals(Object obj) {
        return (this == obj);
    }
+1

Source: https://habr.com/ru/post/1657355/


All Articles