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

Matrix_Multiplication_Lab_Assignment

Java lab study assignment

Uploaded by

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

Matrix_Multiplication_Lab_Assignment

Java lab study assignment

Uploaded by

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

CSE 343 Lab Assignment 3: Matrix Multiplication Using

Multithreading in Java

Objective
The objective of this lab assignment is to understand the concept of multithreading in Java
by implementing 2x2 matrix multiplication (an example given in Figure 1) using both
single-threaded and multi-threaded approaches. You will compare the performance of these
implementations.

Figure 1. A sample 2x2 Matrix Multiplication

Assignment Tasks

Task 1: Generate Matrices


1. Generate two 2 x 2 matrices with random integer values.

Task 2: Single-Threaded Multiplication


1. Implement matrix multiplication using a single thread 10000 times
2. Record the execution time using System.nanoTime(). A sample usage is given below:

import java.time.Duration;
import java.time.Instant;

public class TimerExample {


public static void main(String[] args) {
Instant start = Instant.now(); // Start the timer

// Function call or code to measure


someFunction();

Instant end = Instant.now(); // End the timer


Duration duration = Duration.between(start, end); // Calculate duration

System.out.println("Execution time: " + duration.toNanos() + " nanoseconds");


System.out.println("Execution time: " + duration.toMillis() + " milliseconds");
}

public static void someFunction() {


// Simulate some work with a larger loop
for (int i = 0; i < 100_000_000; i++) {
Math.sqrt(i); // Some calculation
}
}
}

Task 3: Multi-Threaded Multiplication


1. Implement matrix multiplication where each row is computed by a separate thread (do
the multiplication 10000 times).
2. Record the execution time using System.nanoTime().

Task 4: Performance Comparison


1. Compare the execution times of both implementations.

Deliverables
- Source code for single-threaded and multi-threaded implementations.
- Performance comparison report.
- Explanation of results.

You might also like