If we run 3 tasks in this executor then the thread names will bepool-1-thread-1,pool-1-thread-2,pool-1-thread-3. Check out the following example to better understand the naming pattern. importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;publicclassMain{publicstaticvo...
Threads are fundamental building blocks forconcurrent programmingin Java. In many applications, we might need to locate a specific thread by its name to perform operations like debugging, monitoring, or even interacting with the thread’s state. In this tutorial, we’ll explore how to retrieve a...
day26_12(多线程)获取线程名字Thread类方法getName 传智播客 java学习视频,需要文档学习资料的加群628472317
使用Thread.currentThread().getName()和使用this.getName()和对象实例.getName(),都可以得到线程的名称,但是使用this调用getName()方法只能在本类中,而不能在其他类中,更不能在Runnable接口中,所以只能使用Thread.currentThread().getName()获取线程的名称,否则会出现编译时异常。 Thread.currentThread().getName()...
<<Java多线程编程核心技术>> 这本书里说到了这个:Thread.currentThread().getName() this.getName() 他俩是有区别的,得到的效应是不一样的,首先,从直观上来说: Thread.currentThread().getName() 是一个静态方法 this.getName()是一个实例方法
java多线程编程系列-基础1-线程getName和setName的使用 多线程的实现方式有两种: 1.继承Thread类 2.实现了runnable接口 其实在Thread类内部也是实现了Runnable的接口的,写写代码印象更加深刻下面请看: //继承Thread类 package endual; public class MyThread extends Thread{...
String name = Thread.currentThread().getName(); System.out.println(name); } 输出的结果为main。 为什么为main呢? java的项目在启动的时候,会创立一个进程,这个进程同样也是一个线程,在java里面他就叫做main线程。他的名字在设定的时候就是main。我们可以看到上面的代码就是在main方法下执行的,也就是由main...
Thread.currentThread().getName() 1. 和 this.getName() 1. 的区别,前者表示调用当前方法的线程名,后者表示当前线程对象名。可能不是很好理解,根据下面输出解释会比较好理解些。 上列的输出可能性之一如下(线程是随机调度的): MyThreadcurrentThread().getName()=mainMyThreadthis.getName=Thread-1MyThreadcurren...
java.lang.String#indexOf(int)源码: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 /** * Returns the index within this string of the first occurrence of * the specified character. If a character with value * {@code ch} occurs in the character sequence represented by ...
简单来说Thread是线程的意思.current()是个静态方法.意思是返回当前执行这段代码的线程,在我们写的程序中,无论你写的什么程序,都是被拿来执行的(说了句废话),但,执行他的是谁,你简单理解就可以,理解成是线程来执行的.等你以后深入学习了就知道这句话不全对.但对你初学者来说.这样理解就行了.继...