DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET 4
Student Name Aman Bansal UID:22BCS13365
Branch: BE-CSE Section/Group:709-B
Semester: 3rd Date of Performance:31/10/23
Subject Name: Java Programming Subject Code: 22CSH-201
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 {
public static void main(String[] args) {
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);
}
});
Thread oddThread = new Thread(() -> {
System.out.println("Odd No.");
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
for (int i = 1; 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.