diff --git a/answers.txt b/answers.txt new file mode 100644 index 0000000..a9e4d87 --- /dev/null +++ b/answers.txt @@ -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 \ No newline at end of file diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index 2f00c59..78b8d24 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -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> tempMatrixProduct; - public BlockMultiplier() { - // TODO + List> matrix_A; + List> matrix_B; + int startRow; + int endRow; + int startCol; + int endCol; + + public BlockMultiplier(List> matrix_A, List> matrix_B, + List> 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> ParallelizeMatMul(List> matrix_A, List> 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> ParallelizeMatMul(List> matrix_A, + List> matrix_B) { + int p = matrix_A.size(); + int q = matrix_A.get(0).size(); + int r = matrix_B.get(0).size(); + + List> 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> matrix_A = new ArrayList<>(); + List> matrix_B = new ArrayList<>(); + // Initialize matrices A and B (e.g., random values) + + // Call the ParallelizeMatMul method + List> result = ParallelizeMatMul(matrix_A, matrix_B); + + // Display the resulting matrix C + System.out.println("Resulting matrix C:"); + for (List row : result) { + System.out.println(row); + } } -} +} \ No newline at end of file diff --git a/src/main/java/sbu/cs/TaskScheduler.java b/src/main/java/sbu/cs/TaskScheduler.java index 8725c2a..bee603f 100644 --- a/src/main/java/sbu/cs/TaskScheduler.java +++ b/src/main/java/sbu/cs/TaskScheduler.java @@ -1,15 +1,11 @@ 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; @@ -17,35 +13,53 @@ 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 doTasks(ArrayList tasks) - { + public static ArrayList doTasks(ArrayList tasks) { ArrayList 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 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 result = doTasks(tasks); + System.out.println("Order of task execution:"); + for (String taskName : result) { + System.out.println(taskName); + } } }