【问题标题】:getName() vs Thread.currentThread().getName(). Please explain difference between these twogetName() 与 Thread.currentThread().getName()。请解释这两者之间的区别
【发布时间】:2014-10-12 06:57:49
【问题描述】:
public class SynchThread1 extends Thread {
    SynchThread1 st;
    SynchThread1() {}
    SynchThread1(SynchThread1 s) {
        st = s;
    }
    public void run() {
        st.show();
    }
    synchronized void show() {
        for (int i = 0; i < 5; i++)
            System.out.print(Thread.currentThread().getName() + " "); //replace here
    }
    public static void main(String[] args) {
        SynchThread1 s1 = new SynchThread1();
        Thread t1 = new SynchThread1(s1);
        Thread t2 = new SynchThread1(s1);
        Thread t3 = new SynchThread1(s1);
        s1.setName("t0");
        t1.setName("t1");
        t2.setName("t2");
        t3.setName("t3");
        t1.start();
        t2.start();
        t3.start();
    }
}

以上代码的输出为:

t1 t1 t1 t1 t1 t3 t3 t3 t3 t3 t2 t2 t2 t2 t2

但如果我仅将Thread.currentThread().getName() 替换为getName(),则输出为:

t0 t0 t0 t0 t0 t0 t0 t0 t0 t0 t0 t0 t0 t0 t0

请解释为什么会这样。

【问题讨论】:

  • 因为你在搞乱线程,混淆线程和任务(Runnables)。

标签: java multithreading synchronized


【解决方案1】:

因为Thread.currentThread().getName() 为您获取当前运行线程的名称,但getName() 解析为st.getName(),而st 始终是您的第一个线程实例。

为什么getName() 解析为st.getName()?因为:

  1. 在构造第二个到第四个线程期间,将第一个线程作为参数传入并将其保存在实例成员 st 中。

  2. 您的线程的run 方法调用st.show(),因此它们总是在第一个线程上调用show。 (如果你启动了第一个线程,你会在那里得到一个 NPE,因为第一个线程的 st 成员永远不会被赋予非null 值。)

  3. show 内,this 因此是st(第一个线程)。不合格的实例方法调用使用this作为它们的实例,this是第一个线程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多