2.Write a Java program that creates two threads to find and print even and odd numbers from 1 to 20.
💡Code:
class program2 {
public static void main(String[] args) {
Thread evenThread = new Thread(new EvenPrinter());
Thread oddThread = new Thread(new OddPrinter());
evenThread.start();
oddThread.start();
}
}
class EvenPrinter implements Runnable {
public void run() {
for (int i = 2; i <= 20; i += 2) {
System.out.println("Even: " + i);
try {
Thread.sleep(500); // Add delay for better readability
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class OddPrinter implements Runnable {
public void run() {
for (int i = 1; i <= 20; i += 2) {
System.out.println("Odd: " + i);
try {
Thread.sleep(500); // Add delay for better readability
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
📸Output :