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

C9Lab 11 Final

The document outlines a lab session for Object Oriented Programming through Java, focusing on multithreading concepts. It includes pre-lab tasks, a Java program that reads from two text files using threads, and post-lab tasks involving arithmetic operations with a single thread. The session aims to apply theoretical knowledge in practical programming scenarios.

Uploaded by

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

C9Lab 11 Final

The document outlines a lab session for Object Oriented Programming through Java, focusing on multithreading concepts. It includes pre-lab tasks, a Java program that reads from two text files using threads, and post-lab tasks involving arithmetic operations with a single thread. The session aims to apply theoretical knowledge in practical programming scenarios.

Uploaded by

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

23CS3353 − Object Oriented Programming through Java Lab Reg. No.

238W1A05C9

Lab Session 11
Apply the concepts of Threads and Multithreading on a given application
Date of the Session:1/11/2024 Time of the Session:10:20 AM – 1:00 PM
Pre-Lab Tasks:
1. Mention two ways in which thread can be created.

2. Why thread priorities are used?

3. What is synchronization in threads?

4. What is multithreaded programming?

5. Name two distinct types of multitasking.

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No


23CS3353 − Object Oriented Programming through Java Lab Reg. No.238W1A05C9

1. Write a java program using multithreading in which each thread reads the data from a text file and
displays the data of each file on Console alternatively such that one line from first input file is printed
and then one line from another input file is printed and so on.
You are required to develop a java program containing two threads that reads two files simultaneously.
In a program, first thread reads data from file “PersonalRecord.txt” and second thread reads the data
from file “AcademicRecord.txt”. When one thread reads a line from one file then it should allow
another thread to read a line from another file. After reading data from each file, program must write
the output on console (Output screen) such that one line from first input file is printed and then one
line from another input file is printed and so on.

Test Case:
Input:
PersonalRecord.txt
AcademicRecord.txt
Output:
Thread 1:Ajay
Thread 2:DBMS - 96
Thread 2:DLD - 92
Thread 1:9988776655
Thread 2:Java - 95
Thread 1:[email protected]
Thread 2:Data Structures - 94
Thread 1:Vijayawada, Andhra Pradesh
Source Code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class ReadFileThread extends Thread {


private String fileName; // Removed () from fileName
declaration
private static final Object lock = new Object(); // Changed 'look'
to 'lock' and made it 'final'

public ReadFileThread(String fileName) {


this.fileName = fileName;
}

@Override
public void run() {
try (BufferedReader reader = new BufferedReader(new
FileReader(fileName))) {
String line;

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No


23CS3353 − Object Oriented Programming through Java Lab Reg. No.238W1A05C9

while ((line = reader.readLine()) != null) { // Fixed method


name 'readline' to 'readLine'
synchronized (lock) { // Fixed 'Synchronized' to
lowercase 'synchronized'
System.out.println(Thread.currentThread().getName()
+ ": " + line);
lock.notify(); // Notify other waiting thread

try {
lock.wait(); // Wait for the other thread to print its
line
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
synchronized (lock) { // Ensures no thread is left waiting
lock.notify();
}
} catch (IOException e) {
System.err.println("Error reading from file: " +
e.getMessage());
}
}
}

public class Main {


public static void main(String[] args) {
ReadFileThread thread1 = new
ReadFileThread("PersonalRecord.txt");
ReadFileThread thread2 = new
ReadFileThread("AcademicRecord.txt");

thread1.setName("Thread 1");
thread2.setName("Thread 2");

thread1.start();

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No


23CS3353 − Object Oriented Programming through Java Lab Reg. No.238W1A05C9

thread2.stop();
}
}

Output:

Result:
The program to Develop an algorithm to read two text files
concurrently using separate threads, has been executed and run
successfully.

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No


23CS3353 − Object Oriented Programming through Java Lab Reg. No.238W1A05C9

Post-Lab Tasks:
1. Write a program based on performing multiple tasks with a single thread. Task 1 is to perform addition,
Task 2 is to perform subtraction, and Task 3 is to perform multiplication when numbers are given as
20 and 10.
Test Cases:
Output:
Main thread running
Addition of two numbers: 30
Subtraction of two numbers: 10
Multiplication of two numbers: 200
Main thread exiting
Souce Code:

public class SingleThreadTasks {

public static void main(String[] args) {

System.out.println("Main thread running");

int num1 = 20;

int num2 = 10;

// Task 1: Addition

int additionResult = add(num1, num2);

System.out.println("Addition of two numbers: " +


additionResult);

// Task 2: Subtraction

int subtractionResult = subtract(num1, num2);

System.out.println("Subtraction of two numbers: " +


subtractionResult);

// Task 3: Multiplication

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No


23CS3353 − Object Oriented Programming through Java Lab Reg. No.238W1A05C9

int multiplicationResult = multiply(num1, num2);

System.out.println("Multiplication of two numbers: " +


multiplicationResult);

System.out.println("Main thread exiting");

public static int add(int a, int b) {

return a + b;

public static int subtract(int a, int b) {

return a - b;

public static int multiply(int a, int b) {

return a * b;

}
Output:

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No


23CS3353 − Object Oriented Programming through Java Lab Reg. No.238W1A05C9

Result:
The program to Develop an algorithm to use arithmetic operations
(addition, subtraction, and multiplication) on two user-provided
integers using multiple threads. separate threads, has been
executed and run successfully.

Student’s Signature

(For Evaluator’s use only)


Marks Secured: _________ out of __________

Faculty Signature

B. Tech − Computer Science and Engineering (IIIrd Semester) Page No

You might also like