this和Thread.currentThread的区别

本文通过一个Java线程示例详细解释了this与Thread.currentThread()的区别,阐述了两者在不同上下文中所指代的线程实例,并解析了new Thread()内部的工作原理。

public class hello extends Thread {
public hello(){
System.out.println(“Thread.currentThread().getname()=”+Thread.currentThread().getName());
System.out.println(“This.getName=”+this.getName());
}
public void run(){
System.out.println(“Thread.currentThread().getname()=”+Thread.currentThread().getName());
System.out.println(“This.getName=”+this.getName());
}
public static void main(String[] args){
hello thread =new hello();
Thread t1 =new Thread(thread);
t1.setName(“A”);
t1.start();
}
}
得到结果
Thread.currentThread().getname()=main
This.getName=Thread-0
Thread.currentThread().getname()=A
This.getName=Thread-0
为什么这里是Thread-0,不是应该分别得到两个main和两个A吗


说明此时的this和Thread.currentThread()指向不是同一个线程实例

也就是说,this指向的还是new CountOperate()创建的那个线程实例,而不是new Thread(thread)创建的那个实例即t1。
查看源代码可以知道。

实际上new Thread(thread)会将thread应用的对象绑定到一个pravite变量target上,
在t1被执行的时候即t1.run()被调用的时候,它会调用target.run()方法,也就是说它是直接调用thread对象的run方法,
再确切的说,在run方法被执行的时候,this.getName()实际上返回的是target.getName(),而Thread.currentThread().getName()实际上是t1.getName()。

简单来说就是 this指向thread 而 currentThread()指向 t1

<think> Hmm, the user is asking about two lines of C# code related to thread naming and logging. From the code snippet, it seems they're working with multithreading in .NET, specifically setting the current thread's name and then writing thread info. The first line constructs a thread name using string interpolation, combining the managed thread ID with "UI_Dispatcher" - likely indicating this is the main UI thread. The second line triggers some logging mechanism. The user probably wants to understand why you'd do this and what practical purpose it serves. I recall that in debugging scenarios, naming threads makes stack traces and debugger output much more readable. When you have multiple threads, seeing "UI_Dispatcher #1234" is way better than just numbers. The ThreadInfoRecord.WriteInfo() part suggests they're dumping thread state - maybe for debugging deadlocks or performance issues. The user seems technically proficient - they're working at the Thread API level. But I should keep the explanation accessible since thread concepts can get complex. They might be debugging a tricky multithreading issue, given they're examining such low-level details. For the related questions, I'll focus on practical extensions: why name threads, what happens if you don't, how the logging works, and alternatives. These cover both the "what" and "why" behind the code. </think> 这两行代码的作用是: 1. `Thread.CurrentThread.Name = $"{Thread.CurrentThread.ManagedThreadId} UI_Dispatcher";` - 给当前执行线程命名:将托管线程ID拼接字符串"UI_Dispatcher"作为线程名称(例如:`"7 UI_Dispatcher"`) - 核心目的:标识UI调度线程,便于调试时识别主线程或关键线程 2. `ThreadInfoRecord.WriteInfo();` - 调用自定义方法记录线程信息(根据类名推断) - 典型实现可能包含: ```csharp class ThreadInfoRecord { public static void WriteInfo() { Debug.WriteLine($"Thread [{Thread.CurrentThread.Name}] State: {Thread.CurrentThread.ThreadState}"); // 可能记录堆栈跟踪/时间戳等 } } ``` - 作用:输出当前线程状态信息到日志或调试窗口,用于监控线程行为 **主要应用场景**: - 调试多线程程序时快速识别UI线程 - 跟踪线程生命周期状态变化 - 诊断线程阻塞或死锁问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值