EXPERIMENT -07
➢ Multithreading:
#CODE
class A extends Thread
{
public void run()
{
for(int i= 0;i<50;i++)
{
System.out.println("Value of i from A: " + i);
}
System.out.println("Exit from A...");
}
}
class B extends Thread
{
public void run()
{
for(int j= 0;j<50;j++)
{
System.out.println("Value of j from B: " + j);
}
System.out.println("Exit from A...");
}
}
class C extends Thread
{
public void run()
{
for(int k= 0;k<50;k++)
{
System.out.println("Value of k from C: " + k);
}
System.out.println("Exit from A...");
}
}
class D extends Thread
{
public void run()
{
for(int l= 0;l<50;l++)
{
System.out.println("Value of l from D: " + l);
}
System.out.println("Exit from A...");
}
}
public class Main
{
public static void main(String[] args)
{
new A().start();
new B().start();
new C().start();
new D().start();
}
}
#OUTPUT
Value of i from A: 0
Value of i from A: 1
Value of i from A: 2
Value of j from B: 0
Value of j from B: 1
Value of j from B: 2
Value of j from B: 3
Value of j from B: 4
Value of j from B: 5
Value of j from B: 6
Value of l from D: 0
Value of k from C: 0
Value of k from C: 1
Value of k from C: 2
Value of k from C: 3
Value of k from C: 4
Value of k from C: 5
Value of k from C: 6
Value of k from C: 7
Value of k from C: 8
Value of k from C: 9
Value of k from C: 10
Value of l from D: 1
Value of j from B: 7
Value of i from A: 3
Value of j from B: 8
Value of j from B: 9
Value of j from B: 10
Value of j from B: 11
Value of j from B: 12
Value of l from D: 2
Value of l from D: 3
Value of l from D: 4
Value of l from D: 5
Value of l from D: 6
Value of k from C: 11
Value of l from D: 7
Value of l from D: 8
Value of j from B: 13
Value of i from A: 4
Value of j from B: 14
Value of l from D: 9
Value of k from C: 12
Value of l from D: 10
Value of j from B: 15
Value of i from A: 5
Value of j from B: 16
Value of l from D: 11
Value of k from C: 13
Value of l from D: 12
Value of j from B: 17
Value of i from A: 6
Value of j from B: 18
Value of l from D: 13
Value of k from C: 14
Value of l from D: 14
Value of j from B: 19
Value of i from A: 7
Exit from B...
Value of l from D: 15
Value of k from C: 15
Value of l from D: 16
Value of i from A: 8
Value of l from D: 17
Value of l from D: 18
Value of l from D: 19
Exit from D...
Value of k from C: 16
Value of k from C: 17
Value of k from C: 18
Value of k from C: 19
Value of i from A: 9
Exit from C...
Value of i from A: 10
Value of i from A: 11
Value of i from A: 12
Value of i from A: 13
Value of i from A: 14
Value of i from A: 15
Value of i from A: 16
Value of i from A: 17
Value of i from A: 18
Value of i from A: 19
Exit from A...
➢ Packages:
#CODE
package pack1;
public class main
{
public static class A
{
public int a;
public void getA(int num)
{
a=num;
}
public int numSquare()
{
return a*a;
}
public int numCube()
{
return a*a*a;
}
public void display()
{
System.out.println("Square: "+numSquare());
System.out.println("Cube: "+numCube());
}
}
public static class B
{
public int a,b;
public void getB(int num1,int num2)
{
a=num1;
b=num2;
}
public int perimeter()
{
return 2*(a+b);
}
public int area()
{
return a*b;
}
public void display()
{
System.out.println("Perimeter: "+perimeter());
System.out.println("Area: "+area());
}
}
}
package pack2;
import pack1.main.A;
import pack1.main.B;
public class main2
{
public static void main(String[] args)
{
A objA = new A();
objA.getA(3);
objA.display();
B objB = new B();
objB.getB(3,4);
objB.display();
}
}
#OUTPUT
Square: 9
Cube: 27
Perimeter: 14
Area: 12