关于Thread.currentThread().setName(name)和this.setName()的区别

379 阅读2分钟

关于Thread.currentThread().setName(name)和this.setName()的区别

今天在学习Thread时发现在构造方法中使用Thread.currentThread().setName(name);时,名字this.getname()方法获得的名字为Thread-0,只有使用this.setName(name);才可以改名成功

代码

package com.cyc.thread.bank;

public class Depositor extends Thread{
    Account account;
    public Depositor(Account account,String name){
        this.account=account;
        this.setName(name);
//        Thread.currentThread().setName(name);

    }
    @Override
    public void run() {
        for (int i=1;i<=3;i++){
            save();
            try {
                this.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void save(){
        synchronized (account){
            int money =account.wallet;
            money=money+1000;
            System.out.println(Thread.currentThread().getName()+"存了一千银行余额为"+money);
            account.setWallet(money);
        }
    }
}

于是开始实验

Thread.currentThread().setName("sda");加入构造方法中,再在main函数中打印main线程的线程名字代码如下

Thread

package com.cyc.thread.bank;

public class Depositor extends Thread{
    Account account;
    public Depositor(Account account,String name){
        this.account=account;
        this.setName(name);
        Thread.currentThread().setName("sda");

    }
    @Override
    public void run() {
        for (int i=1;i<=3;i++){
            save();
            try {
                this.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void save(){
        synchronized (account){
            int money =account.wallet;
            money=money+1000;
            System.out.println(Thread.currentThread().getName()+"存了一千银行余额为"+money);
            account.setWallet(money);
        }
    }
}

Account

package com.cyc.thread.bank;

public class Account {
    int wallet;

    public int getWallet() {
        return wallet;
    }

    public void setWallet(int wallet) {
        this.wallet = wallet;
    }
}

Main

package com.cyc.thread.bank;

public class Main {
    public static void main(String[] args) {
        Account account = new Account();
        Thread t1=new Depositor(account, "小红");
        Thread t2=new Depositor(account, "小李");
        System.out.println(Thread.currentThread().getName());
        t1.start();
        t2.start();
    }
}

image-20230712191444809.png 从控制台得知,main函数的线程名字为sda,说明在构造方法中使用Thread.currentThread().setName("sda");方法并不能为子线程设置名字而是,而用了this.setName就可以为子线程赋值

image.png 仔细观察代码得知在main主线程调用子线程的构造方法时还是主线程在执行,所以Thread.currentThread().setName("sda");会直接给main函数取名字,而this.setName

是直接给Depositor子线程本身命名

所以得知除去run方法本身和run函数调用的函数中,使用Thread.currentThread().setName("sda");时才是指的是子线程,其他都是main函数

package com.cyc.thread.getname;

public class Main {
    public static void main(String[] args) {
        Thread.currentThread().setName("main");
        System.out.println("currentThread    main   "+Thread.currentThread().getName());
        Thread getName = new GetName();
        getName.start();
    }
}

package com.cyc.thread.getname;

public class GetName extends Thread{
    public GetName(){
        System.out.println("main+GetName   "+Thread.currentThread().getName());
    }
    public void show(){
        System.out.println("show    "+Thread.currentThread().getName());
    }
    @Override
    public void run() {
        Thread.currentThread().setName("run");
        System.out.println("run     "+Thread.currentThread().getName());
        show();
    }
}

image-20230712193738976.png

结论:Thread.currentThread()是获取当前的线程,如果主线程调用子线程时,除去在子线程run函数被调用的方法外,其他都是主线程在调用,如果在构造函数中使用Thread.currentThread().getName()),那么会得到的线程名称是主线程的名称,而使用this.getName,this代表当前对象,所以使用this.getName时获得的就是当前线程对象别名。