Thread for sum and product
class Add extends Thread {
int n;
Add(int n) {
this.n = n;
}
public void run() {
int sum = 0;
for (int i = 1; i <= n; i++) sum += i;
System.out.println("Sum: " + sum);
}
}
class Prod extends Thread {
int n;
Prod(int n) {
this.n = n;
}
public void run() {
int prod = 1;
for (int i = 1; i <= n; i++) prod *= i;
System.out.println("Product: " + prod);
}
}
public class Hello {
public static void main(String[] args) {
Thread t1 = new Add(5);
Thread t2 = new Prod(5);
t1.start();
t2.start();
}
}
Comments
Post a Comment