Sunday, May 5, 2013

Write a Java program that creates three threads. First thread displays “Good Morning” every one second, the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds.


class A extends Thread
{
     synchronized public void run()
     {
    try
    {
        while(true)
        {
           sleep(1000);
           System.out.println("good morning");
        }
    }
    catch(Exception e)
    { }
      }
}
class B extends Thread
{
      synchronized public void run()
      {
    try
    {
        while(true)
        {
        sleep(2000);
        System.out.println("hello");
        }
        }
      catch(Exception e)
    { }
      }
}
class C extends Thread
{
     synchronized public void run()
     {
    try
    {
        while(true)
        {
            sleep(3000);
            System.out.println("welcome");
        }
    }
    catch(Exception e)
    { }
     }
}
class ThreadDemo
{
    public static void main(String args[])
    {
        A t1=new A();
        B t2=new B();
        C t3=new C();
        t1.start();
        t2.start();
        t3.start();
    }
}

Output:

E:\javamani>java ThreadDemo
good morning
good morning
hello
good morning
welcome
good morning
hello
good morning
welcome
hello
good morning
good morning
hello
good morning
welcome
good morning
hello
good morning
good morning
hello
welcome
good morning

5 comments:

This program is go in infinate loop

please correct the program sir

we are getting error at start and sleep methods .... tell me quickly sir

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More