Infinite loop using thread in Java

A thread is just like a program, which has a single flow of control. Threads are called lightweight processes.This is because ll the threads in a main application program share the same address space in the memory.

 

 

 

// Infinite loop using thread in Java

public class PrintMssg extends Thread

{

     String mssg;

     PrintMssg(String mssg)

       {

          this.mssg=mssg;

        }

 public void run()

   {

        try

           {

                for(; ;)

                    {

                          System.out.println("Hello!"+mssg+"Welcome.");

                           sleep(1000);

                     }

          }

 

 catch(Exception erro)

   {

        System.out.println(erro);

    }

 }

 public static void main(String args[])

   {

       PrintMssg thread1=new PrintMssg("Welcome");

       PrintMssg thread2=new PrintMssg("XCnotes.com");

              thread1.start();

              thread2.start();

     }

}

Infinite Loop in run