Aim:- Create a java program with
multithreading. One thread will perform Addition operation and second thread
will perform Subtraction operation.
JAVA Code :-
import java.lang.*;
class Add extends Thread
{
int a,b;
Add(int x,int y)
{
super("Add");
a=x;
b=y;
System.out.println("Add thread: "
+this);
start();
}
public void run()
{
try
{
System.out.println("Addition= "
+(a+b));
sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Add
interrupted ");
}
}
}
class Sub extends Thread
{
int a,b;
Sub(int x,int y)
{
super("Sub");
a=x;
b=y;
System.out.println("Sub thread: "
+this);
start();
}
public void run()
{
try
{
System.out.println("Subtraction= "
+(a-b));
sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Sub
interrupted ");
}
}
}
class AddSum
{
public static void main(String args[])
{
int a,b;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
Add addtion=new Add(a,b);
Sub subtrction=new Sub(a,b);
}
}
Output:-
No comments:
Post a Comment