Write a Thread join() program

Write a Thread join() program

Program
class ThDemo  extends Thread
{
    public void run()  
    {    
        int x;
        for(x=1;x<=3;x++)
        {
            try
            {
                Thread.sleep(2000);
                System.out.println(x);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }   
    
}
public class Main
{
    public static void main(String args[])
    {
        ThDemo d1=new ThDemo();
        ThDemo d2=new ThDemo();
        ThDemo d3=new ThDemo();
        d1.start();
        try
        {
            System.out.println("Current Thread: "+ Thread.currentThread().getName()); 
            d1.join(); 
        } 
        catch(Exception e)
        {
            System.out.println(e);
        }
        d2.start();
        try
        { 
            System.out.println("Current Thread: "+ Thread.currentThread().getName()); 
            d2.join(); 
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        d3.start();
        try
        { 
            System.out.println("Current Thread: "+ Thread.currentThread().getName()); 
            d3.join(); 
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}


Output:

Current Thread: main
1
2
3
Current Thread: main
1
2
3
Current Thread: main
1
2
3


Multi Threading In Java



More Questions


227 . How to count the occurrence of the given character in a string?
228 . How to check if a string is a palindrome?
229 . How to count the number of words in a given string sentence?
230 . How to reverse the words from the given string sentence?
231 . How to swap two strings without using the third variable?
232 . Write a java program to display message a 2sec time gap without using Multi Threading
233 . Write a java program to display message a 2sec time gap using Thread class
234 . Write a java program to display message a 2sec time gap using runnable interface
235 . Write a java program to display addition program and subtraction program with 2 sec time gap
236 . online Ticket Booking program without using Thread Synchronization
237 . Online Ticket Booking program with using synchronized method