Aman Java 4
Aman Java 4
WORKSHEET 4
Aim: Create a program with two threads, one printing even numbers and the other
printing odd numbers up to a certain limit.
1. Source Code:
import java.util.*;
public class Main {
int limit;
System.out.print("Enter Limit:");
Scanner sc= new Scanner(System.in);
int num= sc.nextInt();
limit=num;
Thread evenThread = new Thread(() -> {
System.out.println("Even No.");
for (int i = 2; i <= limit; i += 2) {
System.out.println(i);
}
});
evenThread.start();
oddThread.start();
}
}
3. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Learning Outcomes
a) Basic functions
b) Learnt how to create and start threads using the Thread class and the start()
method.
c) Learnt the importance of synchronization for orderly execution.
d) Demonstrating how multiple threads can work in parallel.