Class 5: Understanding the Current Thread and Thread Names
Learn how to identify the thread currently executing your code and how to give meaningful names to threads.
1. Quick Recap: What Have We Learned So Far?
In our previous classes, we learned how to create threads in Java. We also learned about the Runnable interface and how lambda expressions make it easier to write Runnable tasks.
For example:
Thread thread = new Thread(() -> {
System.out.println("Hello from a new thread");
});
thread.start();
Here, we create a thread and give it a task using a lambda expression.
When we call start(), the new thread starts executing that task.
But how do we know which thread is actually executing a particular line of code?
That's what we will learn today.
2. What Is the Current Thread?
Imagine that your Java program has three threads working at the same time.
Each thread executes its own instructions. At any particular moment, a thread may be executing a specific line of code.
The thread that is currently executing a particular piece of code is called the current thread.
Java provides a simple method to find out which thread is currently executing our code:
Thread.currentThread()
This method belongs to the Thread class. It returns the Thread object representing the thread that is currently executing the code.
3. Understanding Thread.currentThread() with an Example
Let's look at a simple Java program:
public class Main {
public static void main(String[] args) {
System.out.println(Thread.currentThread());
}
}
When we run this program, we will see output similar to:
Thread[main,5,main]
Don't worry about the complete output right now.
The important thing is the word main.
It tells us that the code inside the main() method is executing on a thread named main.
Whenever a Java application starts, Java creates a main thread to execute the main() method.
4. Getting the Name of the Current Thread
Sometimes, we don't need the complete Thread object. We only want to know the name of the thread.
Java provides the getName() method for this purpose.
We can combine it with currentThread() like this:
Thread.currentThread().getName()
Let's see a complete example:
public class Main {
public static void main(String[] args) {
String threadName = Thread.currentThread().getName();
System.out.println("Current thread: " + threadName);
}
}
Output:
Current thread: main
So, we have learned two useful methods:
Thread.currentThread()returns the currently executing Thread object.getName()returns the name of that thread.
5. Giving Names to Threads
When we create threads, Java automatically assigns them names. However, these default names may not tell us what the threads are doing.
Imagine that we are building an application where different threads perform different tasks:
- One thread processes orders.
- Another thread sends emails.
- Another thread generates reports.
It would be much easier to understand our program if the threads had meaningful names.
We can give a thread a name using the setName() method.
For example:
Thread thread = new Thread(() -> {
System.out.println("Hello from a thread");
});
thread.setName("Order-Processing-Thread");
thread.start();
Here, we have assigned the name
Order-Processing-Thread to our thread.
Notice that we call setName() before starting the thread.
This makes the name available when the thread begins executing.
6. Putting Everything Together
Let's create two threads, give them meaningful names, and print the name of the thread executing each task.
public class Main {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
System.out.println(
"Executing on: " +
Thread.currentThread().getName()
);
});
Thread thread2 = new Thread(() -> {
System.out.println(
"Executing on: " +
Thread.currentThread().getName()
);
});
thread1.setName("Order-Thread");
thread2.setName("Email-Thread");
thread1.start();
thread2.start();
}
}
Possible output:
Executing on: Order-Thread
Executing on: Email-Thread
The order of these two lines may change when you run the program. Both threads are started independently, so we should not assume which one will execute first.
The important thing is that each thread prints its own name.
We use Thread.currentThread() from inside the task
to identify the thread executing that task.
7. Can We Set a Thread Name When Creating It?
Yes! The Thread class also provides a constructor that accepts a Runnable task and a thread name.
For example:
Thread thread = new Thread(
() -> {
System.out.println(
Thread.currentThread().getName()
);
},
"Report-Thread"
);
thread.start();
Here, we provide the task as the first argument and the thread name as the second argument.
This is another convenient way to create a named thread.
8. Try It Yourself
Let's practise what we have learned.
Create a Java program with two threads:
- Name the first thread
Student-Thread. - Name the second thread
Teacher-Thread. - Inside each task, print the current thread's name.
- Start both threads.
Try writing the program yourself. You can use either
setName() or the Thread constructor that accepts a name.
9. Test Your Understanding
Answer these five questions to check what you have learned.
Question 1: Which method returns the currently executing Thread object?
Question 2: What does Thread.currentThread().getName() return?
Question 3: Which method can be used to assign a name to a thread?
Question 4: What is the default name of the thread executing the main() method?
Question 5: What does this code do?
Thread thread = new Thread(() -> {
System.out.println(
Thread.currentThread().getName()
);
});
thread.setName("Worker");
thread.start();
10. Class Summary
Let's revise the important points from today's class:
Thread.currentThread()returns the currently executing Thread object.getName()returns the name of a thread.setName()assigns a name to a thread.- The main thread executes the main() method.
- We can also provide a thread name through a Thread constructor.
Naming threads makes our programs easier to understand and helps us identify which thread is performing a particular task.
Coming Up Next: Class 6
In our next class, we will learn about the Thread Lifecycle and Thread States.
We will understand the different states a thread goes through during its lifetime, from creation to completion.
Your Course Progress
0% Completed
You have completed 5 out of 44 classes.