Creating Threads

Aim:
To 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.

Description:
Write a class by extending Thread Class or implementing Runnable interface. Provide run() method in that class. Create three objects to that class. Create three threads using Thread class of lang package and attach single thread to single object for three objects. Call start() method of run() threads.

Program:
class MyThread implements Runnable
{
String str;
int time;
MyThread(String s, int t)
{ str = s;
time = t;
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(str + "\t" + i);
try
{ Thread.sleep(time);
}
catch(Exception e)
{ System.out.println(e);
}
}
}
}
class ThDemo
{
public static void main(String args[])
{
MyThread ob1 = new MyThread("Good Morning", 1000 );
MyThread ob2 = new MyThread("Hello", 2000 );
MyThread ob3 = new MyThread("Welcome", 3000 );
Thread t1 = new Thread(ob1);
Thread t2 = new Thread(ob2);
Thread t3 = new Thread(ob3);
t1.start();
t2.start();
t3.start();
}
}