this.getName(),this.currentThread(),与Thread.currentThread()

this.getName(),this.currentThread(),与Thread.currentThread()的区别

最近学习多线程时,被这几个弄蒙了,现总结如下:

  • 首先看几个Thread类中的几个方法 (注意黑体字部分) :
    • public Thread(Runnable target)。分配新的 Thread 对象。这种构造方法与 Thread(null, target,gname) 具有相同的作用,其中的 gname 是一个新生成的名称。 自动生成的名称的形式为 “Thread-”+n,其中的 n 为整数。
    • public Thread(ThreadGroup group, Runnable target,String name)分配新的 Thread 对象,以便将 target 作为其运行对象,将指定的 name 作为其名称。
  • 直接上代码:
public class CouatentOper extends Thread {
	public CouatentOper(){
		System.out.println("begin");
		System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
		System.out.println("this.currentThread().getName()="+this.currentThread().getName());
		System.out.println("this.getName()="+this.getName());
		System.out.println("end");
	}
	public void run(){
		System.out.println("run begin");
		System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
		System.out.println("this.currentThread().getName()="+this.currentThread().getName());
		System.out.println("this.getName()="+this.getName());
		System.out.println("run end");
	}	
}
public class Run {
	public static void main(String[] args) {
		CouatentOper c = new CouatentOper();
		c.start();
	}
}

结果

begin
Thread.currentThread().getName()=main
this.currentThread().getName()=main

this.getName()=Thread-0
end
run begin
Thread.currentThread().getName()=Thread-0
this.currentThread().getName()=Thread-0
this.getName()=Thread-0
run end

可以发现Thread.currentThread().getName()和this.currentThread().getName()结果相同


public class Run {
	public static void main(String[] args) {
		CouatentOper c = new CouatentOper();
		c.setName("hpc");
		c.start();
	}
}

结果

begin
Thread.currentThread().getName()=main
this.currentThread().getName()=main
this.getName()=Thread-0
end
run begin
Thread.currentThread().getName()=hpc
this.currentThread().getName()=hpc
this.getName()=hpc
run end

setName(String s)的作用是给线程设置名字。如果t1是Thread的子类对象,那么t1.setName(…)改变的是t1的线程的名字,如果t2 = new Thread(t1),那么t2.setName(…)改变的是t2的线程的名字,之所以强调这一点是有原因的,能体现出Thread.currentThread().getName()和this.getName()的不同。上述代码通过setName后显示的都是hpc,是因为只有一个Thread的子类对象,而没有通过Thread(Runnable r)方法分配的对象。看下面代码:

public class Run {
	public static void main(String[] args) {
		CouatentOper c = new CouatentOper();
		c.setName("hpc");
	   Thread t1 = new Thread(c);
		t1.start();
	}
}

结果

begin
Thread.currentThread().getName()=main
this.currentThread().getName()=main
this.getName()=Thread-0
end
run begin
Thread.currentThread().getName()=Thread-1
this.currentThread().getName()=Thread-1
this.getName()=hpc
run end

看见区别了吧,只有this.getName()显示的是hpc(把Thread-0改成了hpc),而Thread.currentThread().getName()显示的是又一个线程的名字(这才正常呀,因为是通过t1开启线程的,而t1又是通过Thread(Runnable r)新分配的线程对象呀)。
所以看Thread.currentThread().getName()和this.currentThread().getName()的结果要弄清楚是通过Thread子类对象开启的线程还是通过new Thread(…)新分配的对象开启的线程(别忘了还有个主线程main).谁开启的就显示谁的线程名字。
而this.getName(),即使线程是通过Thread(Runnable target)新分配的对象开启的,也是将target作为运行对象,所以显示的仍然是Thread子类对象的线程名字。


public class Run {
	public static void main(String[] args) {
		CouatentOper c = new CouatentOper();
	    Thread t1 = new Thread(c);
		t1.setName("hpc");
		t1.start();
	}
}

结果

begin
Thread.currentThread().getName()=main
this.currentThread().getName()=main
this.getName()=Thread-0
end
run begin
Thread.currentThread().getName()=hpc
this.currentThread().getName()=hpc
this.getName()=Thread-0//注意这里的线程名字没有改变,因为对象c的线程名字没有改变。
run end

啰嗦了这么多,重点也就后面的粗体字,笑哭.webp


发布了56 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/hpccph15/article/details/82974467