Skip to content

completed #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions answers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
first question: it will get intrupted and print: Thread was interrupted! first and after that it ends
second question: it will be run normally on the main
third qustion: at first it will create a new thread and start it after that when you call the join method it get back on the main thread
110 changes: 85 additions & 25 deletions src/main/java/sbu/cs/MatrixMultiplication.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,103 @@
package sbu.cs;

import java.util.ArrayList;
import java.util.List;

public class MatrixMultiplication {

// You are allowed to change all code in the BlockMultiplier class
public static class BlockMultiplier implements Runnable
{
public static class BlockMultiplier implements Runnable {
List<List<Integer>> tempMatrixProduct;
public BlockMultiplier() {
// TODO
List<List<Integer>> matrix_A;
List<List<Integer>> matrix_B;
int startRow;
int endRow;
int startCol;
int endCol;

public BlockMultiplier(List<List<Integer>> matrix_A, List<List<Integer>> matrix_B,
List<List<Integer>> tempMatrixProduct,
int startRow, int endRow, int startCol, int endCol) {
this.matrix_A = matrix_A;
this.matrix_B = matrix_B;
this.tempMatrixProduct = tempMatrixProduct;
this.startRow = startRow;
this.endRow = endRow;
this.startCol = startCol;
this.endCol = endCol;
}

@Override
public void run() {
/*
TODO
Perform the calculation and store the final values in tempMatrixProduct
*/
// Calculate the product for the assigned quarter
for (int i = startRow; i < endRow; i++) {
for (int j = startCol; j < endCol; j++) {
int sum = 0;
for (int k = 0; k < matrix_A.get(0).size(); k++) {
sum += matrix_A.get(i).get(k) * matrix_B.get(k).get(j);
}
tempMatrixProduct.get(i).set(j, sum);
}
}
}
}

/*
Matrix A is of the form p x q
Matrix B is of the form q x r
both p and r are even numbers
*/
public static List<List<Integer>> ParallelizeMatMul(List<List<Integer>> matrix_A, List<List<Integer>> matrix_B)
{
/*
TODO
Parallelize the matrix multiplication by dividing tasks between 4 threads.
Each thread should calculate one block of the final matrix product. Each block should be a quarter of the final matrix.
Combine the 4 resulting blocks to create the final matrix product and return it.
*/
return null;
public static List<List<Integer>> ParallelizeMatMul(List<List<Integer>> matrix_A,
List<List<Integer>> matrix_B) {
int p = matrix_A.size();
int q = matrix_A.get(0).size();
int r = matrix_B.get(0).size();

List<List<Integer>> tempMatrixProduct = new ArrayList<>();
for (int i = 0; i < p; i++) {
tempMatrixProduct.add(new ArrayList<>());
for (int j = 0; j < r; j++) {
tempMatrixProduct.get(i).add(0);
}
}

// Divide the quarters among four threads
Thread t1 = new Thread(new BlockMultiplier(matrix_A, matrix_B, tempMatrixProduct,
0, p / 2, 0, r / 2));
Thread t2 = new Thread(new BlockMultiplier(matrix_A, matrix_B, tempMatrixProduct,
0, p / 2, r / 2, r));
Thread t3 = new Thread(new BlockMultiplier(matrix_A, matrix_B, tempMatrixProduct,
p / 2, p, 0, r / 2));
Thread t4 = new Thread(new BlockMultiplier(matrix_A, matrix_B, tempMatrixProduct,
p / 2, p, r / 2, r));

// Start the threads
t1.start();
t2.start();
t3.start();
t4.start();

// Wait for all threads to finish
try {
t1.join();
t2.join();
t3.join();
t4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

return tempMatrixProduct;
}

public static void main(String[] args) {
// Test your code here
// Example usage
// Create matrices A and B (fill in your actual data)
List<List<Integer>> matrix_A = new ArrayList<>();
List<List<Integer>> matrix_B = new ArrayList<>();
// Initialize matrices A and B (e.g., random values)

// Call the ParallelizeMatMul method
List<List<Integer>> result = ParallelizeMatMul(matrix_A, matrix_B);

// Display the resulting matrix C
System.out.println("Resulting matrix C:");
for (List<Integer> row : result) {
System.out.println(row);
}
}
}
}
66 changes: 40 additions & 26 deletions src/main/java/sbu/cs/TaskScheduler.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,65 @@
package sbu.cs;

import java.util.ArrayList;
import java.util.List;

public class TaskScheduler
{
public static class Task implements Runnable
{
/*
------------------------- You don't need to modify this part of the code -------------------------
*/
import java.util.Collections;
import java.util.Comparator;

public class TaskScheduler {
public static class Task implements Runnable {
String taskName;
int processingTime;

public Task(String taskName, int processingTime) {
this.taskName = taskName;
this.processingTime = processingTime;
}
/*
------------------------- You don't need to modify this part of the code -------------------------
*/

@Override
public void run() {
/*
TODO
Simulate utilizing CPU by sleeping the thread for the specified processingTime
*/
try {
// Simulate CPU utilization by sleeping for the specified processing time
Thread.sleep(processingTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Task completed: " + taskName);
}
}

public static ArrayList<String> doTasks(ArrayList<Task> tasks)
{
public static ArrayList<String> doTasks(ArrayList<Task> tasks) {
ArrayList<String> finishedTasks = new ArrayList<>();

/*
TODO
Create a thread for each given task, And then start them based on which task has the highest priority
(highest priority belongs to the tasks that take more time to be completed).
You have to wait for each task to get done and then start the next task.
Don't forget to add each task's name to the finishedTasks after it's completely finished.
*/
// Sort tasks based on processing time (descending order)
Collections.sort(tasks, Comparator.comparingInt(task -> -task.processingTime));

// Create and start threads for each task
for (Task task : tasks) {
Thread thread = new Thread(task);
thread.start();
try {
// Wait for the thread to finish
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Add task name to finishedTasks
finishedTasks.add(task.taskName);
}

return finishedTasks;
}

public static void main(String[] args) {
// Test your code here
// Example usage
ArrayList<Task> tasks = new ArrayList<>();
tasks.add(new Task("Task A", 3000));
tasks.add(new Task("Task B", 2000));
tasks.add(new Task("Task C", 5000));

ArrayList<String> result = doTasks(tasks);
System.out.println("Order of task execution:");
for (String taskName : result) {
System.out.println(taskName);
}
}
}