Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Important Member of java.lang.Thread class

1. public java.lang.Thread();

Access Modifier: - public
Member Type: - Constructor
Parameter: - No Parameter
Functionality: - As we can see, this is the default constructor of the java.lang.Thread class, which allows us to create an object of the java.lang.Thread class.

            
class JTC{
	public static void main(String arg[]){
		Thread t1 = new Thread();
		System.out.println(t1);
	}
}
    Thread[Thread-0,5,main]
            

2. public java.lang.Thread(java.lang.Runnable);

Access Modifier: - public
Member Type: - Constructor
Parameter: - java.lang.Runnable
Functionality: - As we can see, this is a constructor of the java.lang.Thread class that takes a java.lang.Runnable type parameter. In Java, java.lang.Runnable is a functional interface, and we can create a User-Defined thread by implementing the java.lang.Runnable interface. However, when we create an object of its subclass, it does not behave like a thread and cannot follow the Thread Life Cycle. In such cases, we need an object of the Thread class using the java.lang.Thread(java.lang.Runnable) constructor. This constructor creates a Thread class object that is associated with the java.lang.Runnable type object.

            
class Thread1 implements Runnable {
	public void run(){
		System.out.println("run method is processing....");
	}
}
class JTC{
	public static void main(String arg[]){
		Thread1 t1 = new Thread1();
		// t1.start(); --> not okay
		Thread th1 = new Thread(t1);
		th1.start();
	}
}
    run method is processing....
            

In this example, we have a class Thread1 which implements the java.lang.Runnable interface. In class JTC, we create an object of the Thread1 class. However, the main question is how we can start that thread by invoking the start() method of the java.lang.Thread class.
As seen in the next line, we create an object of the java.lang.Thread class using the java.lang.Thread(java.lang.Runnable) constructor and pass the Thread1 class object as an argument. Finally, we start the thread using the start() method of the java.lang.Thread class. This way, the Runnable type object behaves like a thread and follows the standard thread life cycle.

3. public java.lang.Thread(java.lang.String);

Access Modifier: - public
Member Type: - Constructor
Parameter: - java.lang.String
Functionality: - As we know, when we create an object of the java.lang.Thread class, the name of the thread will be Thread-0, Thread-1, and so on, by default. In the Thread class, we have the setName(String) method to set a custom name for the thread as per our business logic. Additionally, using the java.lang.Thread(String) constructor, we can create a thread along with a specific name.

            
class JTC{
	public static void main(String arg[]){
		Thread t1 = new Thread();
		System.out.println("Thread Name :- "+t1.getName());
		t1.setName("T-1");
		System.out.println("Thread Name :- "+t1.getName());
		Thread t2 = new Thread("T-2");
		System.out.println("Thread Name :- "+t2.getName());
	}
}
    Thread Name :- Thread-0
    Thread Name :- T-1
    Thread Name :- T-2
            

4. public static native void yield();

Access Modifier: - public
Member Type: - Static Method
Return Type: - void
Parameter: - yield
Functionality: - As we know, when we create an object of the java.lang.Thread class, the name of the thread will be Thread-0, Thread-1, and so on, by default. In the Thread class, we have the setName(String) method to set a custom name for the thread as per our business logic. Additionally, using the java.lang.Thread(String) constructor, we can create a thread along with a specific name.

5. public final void join() throws java.lang.InterruptedException;

Access Modifier: - public
Member Type: - Final Instance Method
Return Type: - void
Method Name: - join
Functionality: - When we invoke join() along with a thread, the current running thread goes to the waiting state until another (the current working thread of join()) completes its execution.

            
class Thread1 extends Thread{
	

	public void run(){
		try{
			for(int i = 1; i <= 25; i++){
				System.out.println(Thread.currentThread().getName()+" is processing :- "+i);
				Thread.sleep(500);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+" is completed");
	}
}
class Thread2 extends Thread{
	Thread1 t1;
	
	voidsetJoin(Thread1 t1){
		this.t1 = t1;
	}
	public void run(){
		try{
			for(int i = 1; i <= 25; i++){
				System.out.println(Thread.currentThread().getName()+" is processing :- "+i);
				Thread.sleep(500);
				if(i == 15){
					t1.join();
				}
			}
			
				
			
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+" is completed");
	}
}
class JTC{
	public static void main(String arg[]){
		Thread1 t1 = new Thread1();
		Thread2 t2 = new Thread2();
		t1.setName("T-1");
		t2.setName("T-2");
		t1.start();
		t2.start();
		t2.setJoin(t1);
		
	}
}
    T-1 is processing :- 1
    T-2 is processing :- 1
    T-1 is processing :- 2
    T-2 is processing :- 2
    T-2 is processing :- 3
    T-1 is processing :- 3
    T-2 is processing :- 4
    T-1 is processing :- 4
    T-1 is processing :- 5
    T-2 is processing :- 5
    T-1 is processing :- 6
    T-2 is processing :- 6
    T-1 is processing :- 7
    T-2 is processing :- 7
    T-1 is processing :- 8
    T-2 is processing :- 8
    T-1 is processing :- 9
    T-2 is processing :- 9
    T-1 is processing :- 10
    T-2 is processing :- 10
    T-1 is processing :- 11
    T-2 is processing :- 11
    T-1 is processing :- 12
    T-2 is processing :- 12
    T-1 is processing :- 13
    T-2 is processing :- 13
    T-1 is processing :- 14
    T-2 is processing :- 14
    T-1 is processing :- 15
    T-2 is processing :- 15
    T-1 is processing :- 16
    T-1 is processing :- 17
    T-1 is processing :- 18
    T-1 is processing :- 19
    T-1 is processing :- 20
    T-1 is processing :- 21
    T-1 is processing :- 22
    T-1 is processing :- 23
    T-1 is processing :- 24
    T-1 is processing :- 25
    T-1 is completed
    T-2 is processing :- 16
    T-2 is processing :- 17
    T-2 is processing :- 18
    T-2 is processing :- 19
    T-2 is processing :- 20
    T-2 is processing :- 21
    T-2 is processing :- 22
    T-2 is processing :- 23
    T-2 is processing :- 24
    T-2 is processing :- 25
    T-2 is completed
            

In this example, as we can see, when T-2 processes until i=15 and calls join() with T-1, the current thread, which is T-2, moves to the waiting state until T-1 is terminated. Once T-1 comes to the running state, the same sequence can be observed on the console.