1. Overview
1.概述
In this tutorial, we’ll look at different ways to set the name of a Thread in Java. First, we’ll create an example of running two Threads. One prints only even numbers, and the other only odd numbers. Then, we’ll give our Threads a custom name and display them.
在本教程中,我们将探讨在Java中设置Thread名称的不同方法。首先,我们将创建一个运行两个Threads的例子。一个只打印偶数,另一个只打印奇数。然后,我们将给我们的Threads一个自定义的名字并显示它们。
2. Ways to Set Thread Name
2.设置线程名称的方法
A Thread is a lightweight process that can execute concurrently. The Thread class in Java provides a default name for threads.
Thread是一个可以并发执行的轻型进程。Java中的Thread类为线程提供了一个默认名称。
In some cases, we may need to know which thread is running, so giving a custom name to a Thread can make it easier to spot among other running threads.
在某些情况下,我们可能需要知道哪个线程正在运行,所以给一个Thread自定义名称可以使它在其他运行的线程中更容易被发现。
Let’s start by defining a simple class that creates two Threads. The first Thread will print even numbers between 1 and N. The second Thread will print odd numbers between 1 and N. In our example, N is 5.
让我们先定义一个简单的类,创建两个线程。第一个线程将打印1到N之间的偶数,第二个线程将打印1到N之间的奇数。
We’ll also print the Thread default names.
我们还将打印Thread默认的名字。
First, let’s create two Threads:
首先,让我们创建两个线程:。
public class CustomThreadNameTest {
public int currentNumber = 1;
public int N = 5;
public static void main(String[] args) {
CustomThreadNameTest test = new CustomThreadNameTest();
Thread oddThread = new Thread(() -> {
test.printOddNumber();
});
Thread evenThread = new Thread(() -> {
test.printEvenNumber();
});
evenThread.start();
oddThread.start();
}
// printEvenNumber() and printOddNumber()
}
Here, in both the printEvenNumber and printOddNumber methods, we’ll check if the current number is even or odd and print the number along with the Thread name:
在这里,在printEvenNumber和printOddNumber方法中,我们将检查当前数字是偶数还是奇数,并将数字与Thread名称一起打印出来。
public void printEvenNumber() {
synchronized (this) {
while (currentNumber < N) {
while (currentNumber % 2 == 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " --> " + currentNumber);
currentNumber++;
notify();
}
}
}
public void printOddNumber() {
synchronized (this) {
while (currentNumber < N) {
while (currentNumber % 2 == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " --> " + currentNumber);
currentNumber++;
notify();
}
}
}
Running the code gives us the following output:
运行该代码,我们得到以下输出。
Thread-0 --> 1
Thread-1 --> 2
Thread-0 --> 3
Thread-1 --> 4
Thread-0 --> 5
All threads have a default name, Thread-0, Thread-1, and so on.
所有线程都有一个默认的名字,Thread-0, Thread-1,等等。
2.1. Using the Thread Constructor
2.1.使用Thread构造函数
The Thread class provides some constructors where we can provide the Thread name during the Thread creation, such as:
线程类提供了一些构造函数,我们可以在线程创建时提供线程名称,例如。
- Thread(Runnable target, String name)
- Thread(String name)
The parameter name, in this case, is the Thread name.
参数name,在这种情况下,是Thread的名字。
Using the Thread constructor, we can provide the thread name at the thread creation time.
使用Thread构造函数,我们可以在创建线程时提供线程名称。
Let’s give a custom name for our Threads:
让我们为我们的Threads起一个自定义的名字。
Thread oddThread = new Thread(() -> {
test.printOddNumber();
}, "ODD");
Thread evenThread = new Thread(() -> {
test.printEvenNumber();
}, "EVEN");
Now, when we run our code, the custom names are displayed:
现在,当我们运行我们的代码时,自定义的名字就会显示出来。
ODD --> 1
EVEN --> 2
ODD --> 3
EVEN --> 4
ODD --> 5
2.2. Using the setName() Method
2.2.使用setName()方法
Additionally, the Thread class provides a setName method.
此外,Thread类提供了一个setName方法。
Let’s call setName via Thread.currentThread().setName() :
让我们通过Thread.currentThread().setName()来调用setName。
Thread oddThread = new Thread(() -> {
Thread.currentThread().setName("ODD");
test.printOddNumber();
});
Thread evenThread = new Thread(() -> {
Thread.currentThread().setName("EVEN");
test.printEvenNumber();
});
Also, via Thread.setName() :
另外,通过Thread.setName() 。
Thread oddThread = new Thread(() -> {
test.printOddNumber();
});
oddThread.setName("ODD");
Thread evenThread = new Thread(() -> {
test.printEvenNumber();
});
evenThread.setName("EVEN");
Again, running the code shows the custom name of our Threads:
同样,运行该代码显示了我们的Threads的自定义名称。
ODD --> 1
EVEN --> 2
ODD --> 3
EVEN --> 4
ODD --> 5
3. Conclusion
3.总结
In this article, we looked at how we can set the name of a Thread in Java. First, we created a Thread with the default name, then set a custom name using the Thread constructor and later with the setName method.
在这篇文章中,我们研究了如何在Java中设置一个Thread的名字。首先,我们用默认的名字创建了一个Thread,然后用Thread构造函数和后来的setName方法设置一个自定义的名字。
As always, the example code for this article is available over on GitHub.
像往常一样,本文的示例代码可在GitHub上获得。