Introduction
In this article we discuss Joining and Naming of threads.
Thread join() method
This method waits for a thread to die. In other words, we can say it causes the currently running thread to stop executing until the thread it joins completes its task.
Syntax of join() public method
void join()
void join (long mili-seconds)
Let's see an example
In this example, when th1 completes its task th2 and th3 start executing.
MultithreadEx.java
- class MultithreadEx extends Thread {
- public void run() {
- for (int x = 1; x <= 6; x++) {
- try {
- Thread.sleep(400);
- } catch (Exception ex) {
- System.out.println(ex);
- }
- System.out.println(x);
- }
- }
- public static void main(String args[]) {
- MultithreadEx th1 = new MultithreadEx();
- MultithreadEx th2 = new MultithreadEx();
- MultithreadEx th3 = new MultithreadEx();
- th1.start();
- try {
- th1.join();
- } catch (Exception ex) {
- System.out.println(ex);
- }
- th2.start();
- th3.start();
- }
- }
Output
2. Example
In this example; we use the join(long miliseconds) method. So, when th1 completes the task for 2000 miliseconds (5 times) then th2 and th3 start executing.
MultithreadEx1.java
- class MultithreadEx1 extends Thread {
- public void run() {
- for (int x = 1; x <= 6; x++) {
- try {
- Thread.sleep(400);
- } catch (Exception ex) {
- System.out.println(ex);
- }
- System.out.println(x);
- }
- }
- public static void main(String args[]) {
- MultithreadEx1 th1 = new MultithreadEx1();
- MultithreadEx1 th2 = new MultithreadEx1();
- MultithreadEx1 th3 = new MultithreadEx1();
- th1.start();
- try {
- th1.join(2000);
- } catch (Exception ex) {
- System.out.println(ex);
- }
- th2.start();
- th3.start();
- }
- }
Output
How to provide naming of threads
The thread class provides methods to change the name of a thread.
Syntax
public String getName()
public void setName(String sname)
Example
In this example; we use getName(), setName() and getId() methods. This example describes how to change the name of a thread.
- class MultithreadEx2 extends Thread {
- public void run() {
- System.out.println("thread start running.....");
- }
- public static void main(String args[]) {
- MultithreadEx2 th1 = new MultithreadEx2();
- MultithreadEx2 th2 = new MultithreadEx2();
- System.out.println("Name of thread th1:" + th1.getName());
- System.out.println("Name of thread th2:" + th2.getName());
- System.out.println("Id of thread th1:" + th1.getId());
- th1.start();
- th2.start();
- th1.setName("Sandy");
- System.out.println("After rename of thread th1: " + th1.getName());
- }
- }
Output
The currentThread() method
This method returns a reference to the currently executing thread object.
Syntax
public static Thread currentThread()
returns the reference of currently running thread.
Example
- class MutlithreadEx3 extends Thread {
- public void run() {
- System.out.println(Thread.currentThread().getName());
- }
- public static void main(String args[]) {
- MutlithreadEx3 th1 = new MutlithreadEx3();
- MutlithreadEx3 th2 = new MutlithreadEx3();
- th1.start();
- th2.start();
- }
- }
Output