Thread.currentThread()和this

第一个例子

看一个很简单的例子:

public class MyThread extends Thread {

    public MyThread() {
        super();
        System.out.println("构造方法currentThread:"+Thread.currentThread().getName());
        System.out.println("构造方法this:"+this.getName());
    }

    @Override
    public void run() {
        System.out.println("run方法currentThread:"+Thread.currentThread().getName());
        System.out.println("run方法this:"+this.getName());
    }
}
public class TestMain {
    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        myThread.setName("myThread");
        myThread.start();
    }
}

输出如下

构造方法currentThread:main
构造方法this:Thread-0
run方法currentThread:myThread
run方法this:myThread

第一行:构造方法中Thread.currentThread().getName()很明显当前执行的线程是main线程
第二行:构造方法中的this.getName()为什么是Thread-0呢?点进Thread类的无参构造方法中看到源码如下,Thread为每一个线程默认取了一个名字

    public Thread() {
        init(null, null, "Thread-" + nextThreadNum(), 0);
    }

第三行:run方法中Thread.currentThread().getName()),当前执行的线程就是myThread,执行run方法之前我们已经为myThread对象设置了名字,所以name为myThread,谁调用start()方法,currentThread就是谁。
第四行:run方法中的this.getName(),而run方法中this对象就是myThread对象,在这个例子中,我们首先调用了myThread对象的start方法,start方法又回调了myThread类的run方法,所以自始至终都只有myThread对象一个


Thread.currentThread()和this_第1张图片
image.png

第二个例子

MyThread类不变,测试类修改为如下,把myThread对象当做Thread的参数传进去

public class TestMain {
    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        myThread.setName("myThread");
        Thread thread=new Thread(myThread,"thread");
        thread.start();
    }
}

输出如下:

构造方法currentThread:main
构造方法this:Thread-0
run方法currentThread:thread
run方法this:myThread

前两个就不用说了,第三行run方法中currentThread为thread对象,谁调用的start()方法,currentThread就是谁。
第四行run方法中的this为myThread对象,而在这个例子中,我们首先调用了thread对象的start()方法,thread对象的start方法回调了thread类的run方法,thread类的run方法中调用了target的run方法,所以this就指向了target,而target正是我们传入的myThread对象


image.png

image.png

看一下Thread的有参构造方法

    public Thread(Runnable target, String name) {
        init(null, target, name, 0);
    }

再看Thread类的run方法

    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

关于为什么调用了start()方法会执行run()方法参考此篇文章https://blog.csdn.net/qq_38799155/article/details/78488161

你可能感兴趣的:(Thread.currentThread()和this)