0% found this document useful (0 votes)
13 views

Aman Java 4

The document describes a Java program assignment that was completed by the student. The program creates two threads - one to print even numbers and another to print odd numbers up to a user-input limit. The source code defines the main method that gets the limit from the user, creates the two threads, and starts each thread running. The output shows the even and odd numbers printed separately by the respective threads. The student learned how to create and start threads, the importance of synchronization, and how threads can work in parallel.

Uploaded by

downme100times
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Aman Java 4

The document describes a Java program assignment that was completed by the student. The program creates two threads - one to print even numbers and another to print odd numbers up to a user-input limit. The source code defines the main method that gets the limit from the user, creates the two threads, and starts each thread running. The output shows the even and odd numbers printed separately by the respective threads. The student learned how to create and start threads, the importance of synchronization, and how threads can work in parallel.

Uploaded by

downme100times
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

You might also like