Java

 

 

 

 

Java - Thread

 

 

 

 

 

How to Create / Start a Thread

 

There are largely two different ways of creating a thread in Java as below.

  • Creation by extending Thread calss

  • Creation by implementing Runnable Interface

And there are a few different ways of implemeting Runnable Interface as listed below.

  • Implementing as separate class

  • Implementing at the time of declaration : in-line thread

 

I will shows you all the possible examples of thread creation in this section. I don't think you need much explanation for the code itself. Use these examples as a cheatsheet for syntax check when you are creating a thread in your program.

 

 

Following is an example showing how to create a thread by extending Thread class.

 

ThreadEx01.java

 class SampleThread extends Thread {

 

    public void run(){

       System.out.println("SampleThread is running...");

       try{

          System.out.println("Waiting for 5 Seconds...");

          Thread.sleep(5000);

       } catch (Exception e) {

          e.printStackTrace();  

       };

       System.out.println("SampleThread is exiting...");

    }

  }

 

 

public class ThreadEx01 {

 

    public static void main(String[] args) {

        SampleThread st = new SampleThread();

        st.start();

    }

 

}

 

      Output :

       

      SampleThread is running...

      Waiting for 5 Seconds...

      SampleThread is exiting...

 

 

Following is an example showing how to create a thread by implementing Runnable Interface. This is for implementing the interface as a separate class.

 

ThreadEx01.java

 class SampleThread implements Runnable {

 

    public void run(){

       System.out.println("SampleThread is running...");

       try{

          System.out.println("Waiting for 5 Seconds...");

          Thread.sleep(5000);

       } catch (Exception e) {

          e.printStackTrace();  

       };

       System.out.println("SampleThread is exiting...");

    }

  }

 

 

public class ThreadEx01 {

 

    public static void main(String[] args) {

        Thread st = new Thread(new SampleThread());

        st.start();

    }

 

}

 

      Output :

       

      SampleThread is running...

      Waiting for 5 Seconds...

      SampleThread is exiting...

 

 

Following is another example showing how to create a thread by implementing Runnable Interface. This is for implementing the interface at the time of declaration, which is called in-line thread. In this example, the thread gets excuted at the time of declaration.

 

ThreadEx01.java

public class ThreadEx01 {

 

    public static void main(String[] args) {

        (new Thread(new Runnable(){

                public void run(){

                   System.out.println("SampleThread is running...");

                   try{

                      System.out.println("Waiting for 5 Seconds...");

                      Thread.sleep(5000);

                   } catch (Exception e) {

                      e.printStackTrace();  

                   };

                   System.out.println("SampleThread is exiting...");

                }

        })).start();

    }

 

}

 

      Output :

       

      SampleThread is running...

      Waiting for 5 Seconds...

      SampleThread is exiting...

 

 

Following is another example showing how to create a thread by implementing Runnable Interface. This is for implementing the interface at the time of declaration, which is called in-line thread. In this example, the thread is assigned to a variable and gets excuted later.

 

ThreadEx01.java

public class ThreadEx06 {

 

    public static void main(String[] args) {

        Thread st = new Thread(new Runnable(){

                        public void run(){

                           System.out.println("SampleThread is running...");

                           try{

                              System.out.println("Waiting for 5 Seconds...");

                              Thread.sleep(5000);

                           } catch (Exception e) {

                              e.printStackTrace();  

                           };

                           System.out.println("SampleThread is exiting...");

                        }

                    });

        st.start();         

    }

 

}

 

      Output :

       

      SampleThread is running...

      Waiting for 5 Seconds...

      SampleThread is exiting...

 

 

 

How to Check the Status of a Thread

 

 

ThreadEx02.java

 

 class SampleThread extends Thread {

 

    public void run(){

       System.out.println("SampleThread is running...");

       try{

          System.out.println("Waiting for 10 Seconds...");

          Thread.sleep(10000);

       } catch (Exception e) {

          e.printStackTrace();  

       };

       System.out.println("SampleThread is exiting...");

    }

  }

 

 

public class ThreadEx02 {

 

    public static void main(String[] args) {

       

       SampleThread st = new SampleThread();

       st.start();

       

       for(int i = 0; i <= 10; i++) {      

            try{

                Thread.sleep(1000);

                System.out.println("Status of the Thread st = "

                                   + st.getState()

                                   + " : Is the Thread Alive = "

                                   + st.isAlive() );

            } catch (Exception e) {

                e.printStackTrace();    

            };

        

       };

       

    }

 

}

 

      Output :

       

      SampleThread is running...

      Waiting for 10 Seconds...

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true

      SampleThread is exiting...

      Status of the Thread st = TERMINATED : Is the Thread Alive = false

      Status of the Thread st = TERMINATED : Is the Thread Alive = false

 

 

 

Two Threads Running !

 

 

ThreadEx03.java

import java.util.Random;

 

class SampleThread1 extends Thread {

 

    public void run(){

       System.out.println("SampleThread1 is running...");

       Random r = new Random();

       try{

          for(int i =0; i <= 10; i++) {

                System.out.println("SampleThead1 : " + i);

                Thread.sleep(r.nextInt(4000)+500);

          };

       } catch (Exception e) {

          e.printStackTrace();  

       };

       System.out.println("SampleThread1 is exiting...");

    }

}

  

  

class SampleThread2 extends Thread {

 

    public void run(){

       System.out.println("SampleThread2 is running...");

       try{

          for(int i =0; i <= 10; i++) {

                System.out.println("SampleThead2 : " + i);

                Thread.sleep(1000);

          };

       } catch (Exception e) {

          e.printStackTrace();  

       };

       System.out.println("SampleThread2 is exiting...");

    }

}

 

 

public class ThreadEx03 {

 

    public static void main(String[] args) {

       

       SampleThread1 st1 = new SampleThread1();

       SampleThread2 st2 = new SampleThread2();

       

       st1.start();

       st2.start();

       

    }

 

}

 

      Output :

       

      SampleThread1 is running...

      SampleThread2 is running...

      SampleThead2 : 0

      SampleThead1 : 0

      SampleThead1 : 1

      SampleThead2 : 1

      SampleThead2 : 2

      SampleThead1 : 2

      SampleThead2 : 3

      SampleThead2 : 4

      SampleThead1 : 3

      SampleThead2 : 5

      SampleThead1 : 4

      SampleThead2 : 6

      SampleThead2 : 7

      SampleThead2 : 8

      SampleThead1 : 5

      SampleThead2 : 9

      SampleThead2 : 10

      SampleThead1 : 6

      SampleThread2 is exiting...

      SampleThead1 : 7

      SampleThead1 : 8

      SampleThead1 : 9

      SampleThead1 : 10

      SampleThread1 is exiting...

 

 

 

Wait until the Thread execution complete - Thread Join

 

 

ThreadEx04.java

 class SampleThread1 extends Thread {

 

    public void run(){

       System.out.println("SampleThread1 is running...");

       for(int i =0; i <= 1000; i++) {

            if((i % 100) == 0)         

                System.out.println("SampleThead1 : " + i);

                

       };

       System.out.println("SampleThread1 is exiting...");

    }

  }

  

  

   class SampleThread2 extends Thread {

 

    public void run(){

       System.out.println("SampleThread2 is running...");

       for(int i =0; i <= 1000; i++) {

            if((i % 100) == 0)

                System.out.println("SampleThead2 : " + i);

       };

       System.out.println("SampleThread2 is exiting...");

    }

  }

 

 

public class ThreadEx04 {

 

    public static void main(String[] args) {

       

       SampleThread1 st1 = new SampleThread1();

       SampleThread2 st2 = new SampleThread2();

       

       st1.start();

       st2.start();

       

       for(int i =0; i <= 1000; i++) {

            if((i % 50) == 0)

                System.out.println("Main : " + i);

       };

 

    }

 

}

 

 

      Output :

       

      Main : 0

      SampleThread2 is running...

      SampleThread1 is running...

      SampleThead2 : 0

      Main : 50

      SampleThead2 : 100

      SampleThead1 : 0

      SampleThead2 : 200

      Main : 100

      SampleThead2 : 300

      SampleThead1 : 100

      SampleThead2 : 400

      Main : 150

      SampleThead2 : 500

      SampleThead1 : 200

      SampleThead2 : 600

      Main : 200

      SampleThead2 : 700

      SampleThead1 : 300

      SampleThead2 : 800

      Main : 250

      SampleThead2 : 900

      SampleThead1 : 400

      SampleThead2 : 1000

      Main : 300

      SampleThread2 is exiting...

      SampleThead1 : 500

      Main : 350

      SampleThead1 : 600

      Main : 400

      SampleThead1 : 700

      Main : 450

      SampleThead1 : 800

      Main : 500

      SampleThead1 : 900

      Main : 550

      SampleThead1 : 1000

      Main : 600

      SampleThread1 is exiting...

      Main : 650

      Main : 700

      Main : 750

      Main : 800

      Main : 850

      Main : 900

      Main : 950

      Main : 1000

 

 

 

ThreadEx04.java

class SampleThread1 extends Thread {

 

    public void run(){

       System.out.println("SampleThread1 is running...");

       for(int i =0; i <= 1000; i++) {

            if((i % 100) == 0)         

                System.out.println("SampleThead1 : " + i);

                

       };

       System.out.println("SampleThread1 is exiting...");

    }

  }

  

  

class SampleThread2 extends Thread {

 

    public void run(){

       System.out.println("SampleThread2 is running...");

       for(int i =0; i <= 1000; i++) {

            if((i % 100) == 0)

                System.out.println("SampleThead2 : " + i);

       };

       System.out.println("SampleThread2 is exiting...");

    }

  }

 

 

public class ThreadEx04 {

 

    public static void main(String[] args) {

       

       SampleThread1 st1 = new SampleThread1();

       SampleThread2 st2 = new SampleThread2();

       

       

       st1.start();

       try{

           st1.join();

       } catch (Exception e) {     

           e.printStackTrace(); 

       };

       

       st2.start();

       try{

           st2.join();

       } catch (Exception e) {     

           e.printStackTrace(); 

       };

 

       for(int i =0; i <= 1000; i++) {

            if((i % 50) == 0)

                System.out.println("Main : " + i);

       };

    }

 

}

 

 

      Output :

       

      SampleThread1 is running...

      SampleThead1 : 0

      SampleThead1 : 100

      SampleThead1 : 200

      SampleThead1 : 300

      SampleThead1 : 400

      SampleThead1 : 500

      SampleThead1 : 600

      SampleThead1 : 700

      SampleThead1 : 800

      SampleThead1 : 900

      SampleThead1 : 1000

      SampleThread1 is exiting...

      SampleThread2 is running...

      SampleThead2 : 0

      SampleThead2 : 100

      SampleThead2 : 200

      SampleThead2 : 300

      SampleThead2 : 400

      SampleThead2 : 500

      SampleThead2 : 600

      SampleThead2 : 700

      SampleThead2 : 800

      SampleThead2 : 900

      SampleThead2 : 1000

      SampleThread2 is exiting...

      Main : 0

      Main : 50

      Main : 100

      Main : 150

      Main : 200

      Main : 250

      Main : 300

      Main : 350

      Main : 400

      Main : 450

      Main : 500

      Main : 550

      Main : 600

      Main : 650

      Main : 700

      Main : 750

      Main : 800

      Main : 850

      Main : 900

      Main : 950

      Main : 1000

 

 

 

ThreadEx04.java

class SampleThread1 extends Thread {

 

    public void run(){

       System.out.println("SampleThread1 is running...");

       for(int i =0; i <= 1000; i++) {

            if((i % 100) == 0)         

                System.out.println("SampleThead1 : " + i);

                

       };

       System.out.println("SampleThread1 is exiting...");

    }

  }

  

  

class SampleThread2 extends Thread {

 

    public void run(){

       System.out.println("SampleThread2 is running...");

       for(int i =0; i <= 1000; i++) {

            if((i % 100) == 0)

                System.out.println("SampleThead2 : " + i);

       };

       System.out.println("SampleThread2 is exiting...");

    }

  }

 

 

public class ThreadEx04 {

 

    public static void main(String[] args) {

       

       SampleThread1 st1 = new SampleThread1();

       SampleThread2 st2 = new SampleThread2();

       

       

       st1.start();

       st2.start();

       

       try{

           st1.join();

           st2.join();

       } catch (Exception e) {     

           e.printStackTrace(); 

       };

 

       for(int i =0; i <= 1000; i++) {

            if((i % 50) == 0)

                System.out.println("Main : " + i);

       };

    }

 

}

 

 

      Output :

       

      SampleThread1 is running...

      SampleThread2 is running...

      SampleThead2 : 0

      SampleThead1 : 0

      SampleThead2 : 100

      SampleThead1 : 100

      SampleThead2 : 200

      SampleThead1 : 200

      SampleThead2 : 300

      SampleThead1 : 300

      SampleThead2 : 400

      SampleThead1 : 400

      SampleThead2 : 500

      SampleThead1 : 500

      SampleThead2 : 600

      SampleThead1 : 600

      SampleThead2 : 700

      SampleThead1 : 700

      SampleThead2 : 800

      SampleThead1 : 800

      SampleThead2 : 900

      SampleThead1 : 900

      SampleThead2 : 1000

      SampleThead1 : 1000

      SampleThread2 is exiting...

      SampleThread1 is exiting...

      Main : 0

      Main : 50

      Main : 100

      Main : 150

      Main : 200

      Main : 250

      Main : 300

      Main : 350

      Main : 400

      Main : 450

      Main : 500

      Main : 550

      Main : 600

      Main : 650

      Main : 700

      Main : 750

      Main : 800

      Main : 850

      Main : 900

      Main : 950

      Main : 1000

 

 

 

 

Reference :