Wednesday, November 21, 2012

Multliplication Using Multithread in java

Multliplication Using Multithread in java



import java.util.*;
class FirstThread implements Runnable
{
Thread t;
FirstThread()
{
t=new Thread(this,"First Thread");
System.out.println("First Thread:"+t);
t.start();
}
public void run()
{
try
{
System.out.println("Two's Multiplication Table ");
for(int i=1;i<=5;i++)
{
System.out.println(+i+"*2="+(2*i));
Thread.sleep(100);
}
}
catch(InterruptedException e)
{
System.out.println("First Thread interrupted");
}
System.out.println("First Thread existing");
}
}

class SecondThread implements Runnable
{
Thread t;
SecondThread()
{
t=new Thread(this,"Second Thread");
System.out.println("second thread:"+t);
t.start();
}
public void run()
{
try
{
System.out.println("Five's multiplication table");
for(int i=1;i<=5;i++)
{
System.out.println(+i+"*5="+(5*i));
{
Thread.sleep(101);
}
}
}
catch(InterruptedException e)
{
System.out.println("second thread interupted");
}
System.out.println("second thread existing");
}
}

class ThirdThread implements Runnable
{
Thread t;
ThirdThread()
{
t=new Thread(this ,"Third Thread");
System.out.println("Third Thread:"+t);
t.start();
}
public void run()
{
try
{
System.out.println("Ten's multiplication table ");
for(int i=1;i<=5;i++)
{
System.out.println(+i+"*10="+(10*i));
Thread.sleep(102);
}
}
catch(InterruptedException e)
{
System.out.println("third thread interrupted");
}
System.out.println("third thread existing");
}
}

class MainThread
{
public static void main(String args[])
{
FirstThread ob1=new FirstThread();
SecondThread ob2=new SecondThread();
ThirdThread ob3=new ThirdThread();
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("main thread interrupted");
}
System.out.println("main thread existing");
}
}

0 comments:

Post a Comment