Program 1 :
public class thr1 extends Thread
{
public void run()
{
System.out.println("thread runing");
}
public static void main (String arg[])
{
thr1 x= new thr1();
x.start();
}
}
Program 2 :
// by implementig runable interface
public class thr2 implements Runnable
{
public void run()
{
System.out.println("thread runing");
}
public static void main (String arg[])
{
thr2 x= new thr2();
Thread t=new Thread(x);
t.start();
}
}
Program 3 :
public class thr3 extends Thread
{ int i;
public void run()
{
for (i=1;i<15;i++)
{
System.out.println(i);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main (String arg[])
{
thr3 x= new thr3();
x.start();
}
}
Program 4 :
// sleep method
public class thr4 extends Thread
{ int i;
public void run()
{
for (i=1;i<10;i++)
{
System.out.println(i);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main (String arg[])
{
thr3 x1= new thr3();
thr3 x2= new thr3();
thr3 x3= new thr3();
x1.start();
try
{
x1.join();
}
catch(Exception e)
{
System.out.println(e);
}
x2.start();
try
{
x2.join();
}
catch(Exception e)
{
System.out.println(e);
}
x3.start();
}
}
No comments:
Post a Comment