Java Naming Thread and Current Thread28 Mar 2025 | 2 min read Naming ThreadThe Thread class provides methods to change and get the name of a thread. By default, each thread has a name, i.e. thread-0, thread-1 and so on. By we can change the name of the thread by using the setName() method. The syntax of setName() and getName() methods are given below: We can also set the name of a thread directly when we create a new thread using the constructor of the class. Example of naming a thread : Using setName() MethodFileName: TestMultiNaming1.java Test it NowOutput: Name of t1:Thread-0 Name of t2:Thread-1 After changing name of t1:Sonoo Jaiswal running... running... Example of naming a thread : Without Using setName() MethodOne can also set the name of a thread at the time of the creation of a thread, without using the setName() method. Observe the following code. FileName: ThreadNamingExample.java Output: Thread - 1: JavaTpoint1 Thread - 2: JavaTpoint2 The thread is executing.... The thread is executing.... Current ThreadThe currentThread() method returns a reference of the currently executing thread. Example of currentThread() methodFileName: TestMultiNaming2.java Test it NowOutput: Thread-0 Thread-1 Next TopicThread-priority-in-java |
How to perform single task by multiple threads in Java? If you have to perform a single task by many threads, have only one run() method. For example: Program of performing single task by multiple threads FileName: TestMultitasking1.java class TestMultitasking1 extends Thread{ public void run(){ System.out.println("task one"); } ...
3 min read
is a process of executing multiple threads simultaneously. A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. However, we use multithreading rather than multiprocessing because threads use a shared memory area. It does not allocate...
9 min read
Java Thread pool represents a group of worker threads that are waiting for the job and reused many times. In the case of a thread pool, a group of fixed-size threads is created. A thread from the thread pool is pulled out and assigned a job...
8 min read
The Java Thread class provides the two variants of the sleep() method. First one accepts only an argument, whereas the other variant accepts two arguments. The method sleep() is being used to halt the working of a thread for a given amount of time. The time...
6 min read
(Thread States) In Java, a thread always exists in any one of the following states. These states are: New Active Blocked / Waiting Timed Waiting Terminated Explanation of Different Thread States New: Whenever a new thread is created, it is always in the new state. For a thread in the new state,...
7 min read
Each thread has a priority. Priorities are represented by a number between 1 and 10. In most cases, the thread scheduler schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which...
4 min read
What if we call Java run() method directly instead start() method? Each thread starts in a separate call stack. Invoking the run() method from the main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack. FileName: TestCallRun1.java class...
1 min read
Daemon thread in Java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically. There are many java daemon threads running automatically e.g....
2 min read
How to Create a Thread in Java? Multithreading is a fundamental concept in Java programming, allowing developers to execute multiple tasks concurrently within a single program. Threads are lightweight processes that run within the context of a larger process, enabling efficient utilization of system resources and...
5 min read
The join() method in Java is provided by the java.lang.Thread class that permits one thread to wait until the other thread to finish its execution. Suppose th be the object the class Thread whose thread is doing its execution currently, then the th.join(); statement ensures...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India