Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

DEEPASNHU 19BCE0174

Deepanshu wadhwa 19BCE0174

LAB FAT JAVA

Q5

CODE
import java.util.*;

public class Main

static boolean isPrime(int n)

// Corner case

if (n <= 1)

return false;

// Check from 2 to n-1

for (int i = 2; i < n; i++)

if (n % i == 0)

return false;

return true;

}
DEEPASNHU 19BCE0174

public static void main(String[] args) throws InterruptedException{

System.out.println("Hello World");

LinkedList<Integer> list = new LinkedList<Integer>();

ArrayList<Integer> a1 = new ArrayList<Integer>();

ArrayList<Integer> a2 = new ArrayList<Integer>();

Runnable r1 = () -> {

for(int i=0; i<=100; i++)

list.add(i);

};

Thread t1 = new Thread(r1);

Runnable r2 = () -> {

for(int i=0; i<=100; i++)

if(isPrime(list.get(i)))

a1.add(list.get(i));

else

a2.add(list.get(i));

};
DEEPASNHU 19BCE0174

Thread t2 = new Thread(r2);

Runnable r3 = () -> {

System.out.println("Prime: ");

System.out.println(a1);

System.out.println("Non Prime: ");

System.out.println(a2);

};

Thread t3 = new Thread(r3);

t1.start();

t1.join();

t2.start();

t2.join();

t3.start();

t3.join();

}
DEEPASNHU 19BCE0174

You might also like