如何比较线程对象 [英] How to compare thread objects

查看:69
本文介绍了如何比较线程对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了AWT的 EventQueue 在这里看到了这段代码:

I recently came across the sources of AWT's EventQueue where 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();
    }
}

真正困扰我的是使用==比较线程对象.到目前为止,我一直在使用equals(Object)进行此操作.我已经看过这个问题,但是两个答案并不是我真正想要的.

What really struggles me is that the thread objects are being compared using ==. So far I've been doing this with equals(Object). I already had a look at this question but the two answers aren't really what I'm looking for.

两个不同的实例是否可能引用同一个本机线程?我应该如何比较线程对象的相等性?

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

推荐答案

两个不同的实例是否可能引用同一个本机线程?

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

否.

根据Thread javadoc:

According to the Thread javadoc:

线程是程序中的执行线程.

A thread is a thread of execution in a program.

Thread对象的生命周期分为三个阶段:

The life cycle of a Thread object has three phases:

  • start()调用之前,Thread对象表示尚未创建的 thread .并且可能永远不会被创建;即是否未调用start(). (此时,没有本机线程.)

  • Prior to the start() call, a Thread object denotes a thread that has yet to be created. And might never be created; i.e. if start() isn't called. (At this point, there is no native thread.)

start()调用之后,直到run()调用终止,Thread对象表示一个活动的 thread . (这时有一个本机线程.)

After the start() call, and until the run() call terminates, the Thread object denotes a live thread. (At this point, there is a native thread.)

run()调用终止后,Thread对象表示不再存在的 thread . (这时包含 thread 的本机线程已被删除.)

After the run() call terminates, the Thread object denotes a thread that no longer exists. (At this point the native thread that embodied the thread has been deleted.)

两个不同的Thread对象表示相同的 thread 毫无意义.即同一执行线程.

At no point does it make any sense for two distinct Thread objects to denote the same thread; i.e. the same thread of execution.

我应该如何比较线程对象的相等性?

How should I compare thread objects for equality?

使用==是正确的方法.

但是equals(Object)也是正确的,因为Thread是从Object继承而来的,而Object被定义为与==相同.

But equals(Object) is also correct, because Thread inherits it from Object where it is defined to be the same as ==.

在风格上.

有人认为,从风格上讲,使用equals更好.但是,在这种情况下,Thread javadoc(实际上)指定了equals==做相同的事情.实际上,引用相等是唯一对Thread对象有意义的相等语义.这是基于`线程生命周期的工作方式,以及两个截然不同的执行线程的事实. (他们可以始终如一地产生相同的结果,但这就是紧急"行为……并证明该行为通常是一个棘手的问题.)

Some would argue that is is stylistically better to use equals. However, it this context, the Thread javadoc (in effect) specifies that equals and == do the same thing. Indeed, reference equality is the only equality semantics that would make any sense for Thread objects. This follows from the way that the `Thread life-cycle works, and the fact two distinct threads of execution intuitively. (They could consistently produce the same results, but that is "emergent" behavior ... and proving that behavior is in general an intractable problem.)

另一方面,这个问题与风格无关.关于==在这种情况下在语义上是否正确.

On the other hand, this question wasn't about style. It was about whether == is semantically correct in this context.

这篇关于如何比较线程对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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