In this article, we will learn toset and get thread names in Javawith simple examples using the built-in methodssetName()andgetName()in theThreadclass. 1. Getting Thread Name By default, the Java compiler sets a default name of each threadwhile creating, and we can get the thread name ...
一、Java获取当前类名和方法名Thread.currentThread().getStackTrace() 获取class名:this.getClass().getName(); 或者 Thread.currentThread().getStackTrace()[1].getClassName(); 获取方法名: Thread.currentThread().getStackTrace()[1].getMethodName(); 获取行号: Thread.currentThread().getStackTrace()[1]....
使用Thread.currentThread().getName()和使用this.getName()和对象实例.getName(),都可以得到线程的名称,但是使用this调用getName()方法只能在本类中,而不能在其他类中,更不能在Runnable接口中,所以只能使用Thread.currentThread().getName()获取线程的名称,否则会出现编译时异常。 Thread.currentThread().getName()...
importjava.lang.management.ThreadInfo;//导入方法依赖的package包/类privatevoidprintThread(ThreadInfo ti, PrintStream out){ StringBuilder sb =newStringBuilder("\""+ ti.getThreadName() +"\""+// NOI18N" Id="+ ti.getThreadId() +// NOI18N" in "+ ti.getThreadState());// NOI18Nif(ti.getL...
简单来说Thread是线程的意思.current()是个静态方法.意思是返回当前执行这段代码的线程,在我们写的程序中,无论你写的什么程序,都是被拿来执行的(说了句废话),但,执行他的是谁,你简单理解就可以,理解成是线程来执行的.等你以后深入学习了就知道这句话不全对.但对你初学者来说.这样理解就行了.继...
Thread.currentThread().getName() 1. 和 this.getName() 1. 的区别,前者表示调用当前方法的线程名,后者表示当前线程对象名。可能不是很好理解,根据下面输出解释会比较好理解些。 上列的输出可能性之一如下(线程是随机调度的): MyThreadcurrentThread().getName()=mainMyThreadthis.getName=Thread-1MyThreadcurren...
Java Thread getName()用法及代码示例线程类的 getName() 方法用于返回线程的名称。 用法 public final String getName() 返回 This method returns the name of thread. 示例 public class GetNameExample extends Thread { public void run() { System.out.println("Thread is running..."); } public ...
new Thread( -> { try { Object data ="-公众号Java技术栈AAA"; System.out.println(Thread.currentThread.getName + data); // 开始交换数据 data = exchanger.exchange(data, 3000L, TimeUnit.MILLISECONDS); System.out.println(Thread.currentThread.getName + data); ...
name = name; } @Override public void run() { System.out.println("Executing " + name); } } Output: Thread1's ID is: 13 Thread2's ID is: 14 Executing Task 2 Executing Task 1 Get Current Thread Pool Id Using Thread.currentThread().getId() in Java Thread pools are beneficial ...
Namespace: Java.Interop Assembly: Java.Interop.dll C# 複製 public virtual string? GetCurrentManagedThreadName (); Returns String Remarks Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the ...