Java provides a bunch of methods to manage threads that we create. Few of most important methods are listed down below.
| Method | Usage |
| getName() | Returns the thread name |
| getPriority() | Returns the thread priority |
| sleep() | Suspend a thread for a period of time |
| join() | Wait for a thread to terminate |
| isAlive() | Determines if the thread is still running |
Now let’s see how each of these methods are working with examples.
getName() Method
public class ThreadMethods {
public static void main(String[] args) {
Thread threadOne = new Thread();
Thread threadTwo = new Thread();
System.out.println(threadOne.getName());
System.out.println(threadTwo.getName());
}
}
OUTPUT>>
Thread-0
Thread-1
similarly, we can set the name of a thread as follows
threadOne.setName("The ThreadOne");
threadTwo.setName("The ThreadTwo");
getPriority() Method
The concurrency happens among multiple threads are based on their priorities. These priority values are ranging from 1 to 10 where the value 1 is considered as a low priority and the value 10 is the maximum.
public class ThreadMethods {
public static void main(String[] args) {
Thread threadOne = new Thread();
Thread threadTwo = new Thread();
threadOne.setName("The ThreadOne");
threadTwo.setName("The ThreadTwo");
System.out.println(threadOne.getPriority());
System.out.println(threadTwo.getPriority());
}
}
since this is a normal case, the output will be default which is 5 for both of these two threads.
Based on the integer value, there are three main priorities.
- MIN_PRIORITY :- which contains the value 1 (low priority).
- NORM_PRIORITY :- which contains the value 5 (default priority).
- MAX_PRIORITY :- which contains the value 10 (maximum priority).
The following program shows you the way of multi-threaded programs are behaved in the order of thread’s execution when their priorities are different.
public class ThreadMethods extends Thread{
public static void main(String[] args) {
Runnable task = () -> System.out.println(currentThread().getName());
Thread threadOne = new Thread(task);
Thread threadTwo = new Thread(task);
threadOne.setPriority(MIN_PRIORITY);
threadTwo.setPriority(MAX_PRIORITY);
threadOne.start();
threadTwo.start();
}
}
OUTPUT>>
Thread-1
Thread-0
since the threadTwo has the max priority, it will be executed first.
Sleep() Method
The sleep() method causes the thread from which it is called to suspend execution for a specified period of milliseconds.
The following program shows you how you can run one for loop on two threads concurrently.
public class ThreadMethods Thread{
public static void main(String[] args) {
Runnable task = ()->{
for(int i = 0; i<10; i++){
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread threadOne = new Thread(task);
Thread threadTwo = new Thread(task);
threadOne.start();
threadTwo.start();
}
}
here I have used the sleep() method to stop a thread for 1 second, while the thread stops its execution the other thread will be executed. This happens until the for loop finishes its routines.
IsAlive() Method
public class Hello {
public static void main(String[] args) throws InterruptedException {
Runnable task = ()-> {
for(int i = 0; i<10; i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread threadOne = new Thread(task);
Thread threadtwo = new Thread(task);
threadOne.start();
System.out.println(threadOne.isAlive());
System.out.println(threadTwo.isAlive());
threadTwo.start();
System.out.println(threadOne.isAlive());
System.out.println(threadTwo.isAlive());
Thread.sleep(1100);
System.out.println(threadOne.isAlive());
System.out.println(threadTwo.isAlive());
}
}
OUTPUT>>
true
false
true
true
false
false
Here I have created 2 threads. In the run() method, a simple for loop is executing from 0 to 9 which stops 100 milliseconds time period for each loop. The threadOne starts before the threadTwo that causes the isAlive() method for the threadOne to be true whereas false for the threadTwo(line 19, 20).
Then the threadTwo starts its execution while the threadOne is running, since the for loop in the run() method needs more time to finish its works. Now, both the threads are alive which prints true for isAlive() methods(line 23,24).
Then the main thread sleeps for 1100 milliseconds which stops the main thread until the threadOne and threadTwo terminate their execution. After that both of these threads will not be alive, the output will be false.
Join() method.
Consider the following program.
public class Hello {
public static void main(String[] args) throws InterruptedException {
Runnable task = ()-> System.out.println("");
Thread threadOne = new Thread(task);
System.out.println(threadOne.isAlive());
threadOne.start();
threadOne.join();
System.out.println(threadOne.isAlive());
}
}
In the above program, the the join() method helps the main thread to wait until the threadOne finishes its execution. So, the join() method is used to hold the program in execution until a particular thread dies.