diff --git a/answer.md b/answer.md new file mode 100644 index 0000000..b694c0e --- /dev/null +++ b/answer.md @@ -0,0 +1,29 @@ +# Fifth-Assignment-Multithread Theoretical Questions 📝 answers + +## Question 1: + +1. interrupt() method: If any thread is in sleeping or waiting for a state then using the interrupt() method, we can interrupt the execution of that thread by showing InterruptedException. A thread that is in the sleeping or waiting state can be interrupted with the help of the interrupt() method of Thread class. + +2. in this code sleep get ignored and thread work without sleep. +### outPut: +Thread was interrupted! + +Thread will be finished here!!! + + +## Question 2: +1. the run() method is called directly, so the message will be printed from the main thread +2. run() in this code exactly worked like function +3. code running step by step + +### outPut: +Running in main + +## Question 3: +1. The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t. join(); causes the current thread to pause execution until t 's thread terminates. + +### outPut: +Running in: Thread-0 + +Back to: main + diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index 2f00c59..902cc96 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -1,5 +1,6 @@ package sbu.cs; +import java.util.ArrayList; import java.util.List; public class MatrixMultiplication { @@ -8,16 +9,40 @@ public class MatrixMultiplication { public static class BlockMultiplier implements Runnable { List> tempMatrixProduct; - public BlockMultiplier() { - // TODO + int num; + List> matrix_A, matrix_B; + public BlockMultiplier(int num, List> matrix_A, List> matrix_B) { + this.num = num; + this.matrix_A = matrix_A; + this.matrix_B = matrix_B; + tempMatrixProduct = new ArrayList<>(); } @Override public void run() { - /* - TODO - Perform the calculation and store the final values in tempMatrixProduct - */ + int x = 0, y = 0; + int p, q, r; + p = matrix_A.size(); + q = matrix_B.size(); + r = matrix_B.get(0).size(); + if (num == 2 || num == 4) + y = r / 2; + if (num == 3 || num == 4) + x = p / 2; + for (int i = 0; i < p / 2; i++) { + List satr = new ArrayList<>(); + for (int j = 0; j < r / 2; j++) { + int ans = 0; + for (int k = 0; k < q; k++) { + ans += matrix_A.get(x + i).get(k) * matrix_B.get(k).get(y + j); + } + satr.add(ans); + } + tempMatrixProduct.add(satr); + } + } + public List> getTempMatrixProduct() { + return tempMatrixProduct; } } @@ -28,16 +53,60 @@ public void run() { */ 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; + List> ParallelizeMatMul = new ArrayList<>(); + + List> mat1 = new ArrayList<>(); + List> mat2 = new ArrayList<>(); + List> mat3 = new ArrayList<>(); + List> mat4 = new ArrayList<>(); + + BlockMultiplier BM1 = new BlockMultiplier(1, matrix_A, matrix_B); + BlockMultiplier BM2 = new BlockMultiplier(2, matrix_A, matrix_B); + BlockMultiplier BM3 = new BlockMultiplier(3, matrix_A, matrix_B); + BlockMultiplier BM4 = new BlockMultiplier(4, matrix_A, matrix_B); + + Thread thread1 = new Thread(BM1); + Thread thread2 = new Thread(BM2); + Thread thread3 = new Thread(BM3); + Thread thread4 = new Thread(BM4); + + thread1.start(); + thread2.start(); + thread3.start(); + thread4.start(); + + try { + thread1.join(); + mat1 = BM1.getTempMatrixProduct(); + thread2.join(); + mat2 = BM2.getTempMatrixProduct(); + thread3.join(); + mat3 = BM3.getTempMatrixProduct(); + thread4.join(); + mat4 = BM4.getTempMatrixProduct(); + }catch (InterruptedException e) { + System.out.println("thread Interrupted."); + } + for (int i = 0; i < mat1.size(); i++) { + List satr = new ArrayList<>(); + for (int j = 0; j < mat1.get(0).size(); j++) + satr.add(mat1.get(i).get(j)); + for (int j = 0; j < mat2.get(0).size(); j++) + satr.add(mat2.get(i).get(j)); + ParallelizeMatMul.add(satr); + } + for (int i = 0; i < mat3.size(); i++) { + List satr = new ArrayList<>(); + for (int j = 0; j < mat3.get(0).size(); j++) + satr.add(mat3.get(i).get(j)); + for (int j = 0; j < mat4.get(0).size(); j++) + satr.add(mat4.get(i).get(j)); + ParallelizeMatMul.add(satr); + } + return ParallelizeMatMul; } public static void main(String[] args) { - // Test your code here + } } diff --git a/src/main/java/sbu/cs/TaskScheduler.java b/src/main/java/sbu/cs/TaskScheduler.java index 8725c2a..4753719 100644 --- a/src/main/java/sbu/cs/TaskScheduler.java +++ b/src/main/java/sbu/cs/TaskScheduler.java @@ -1,6 +1,7 @@ package sbu.cs; import java.util.ArrayList; +import java.util.Collection; import java.util.List; public class TaskScheduler @@ -12,7 +13,6 @@ public static class Task implements Runnable */ String taskName; int processingTime; - public Task(String taskName, int processingTime) { this.taskName = taskName; this.processingTime = processingTime; @@ -23,29 +23,43 @@ public Task(String taskName, int processingTime) { @Override public void run() { - /* - TODO - Simulate utilizing CPU by sleeping the thread for the specified processingTime - */ + try { + Thread.sleep(processingTime); + } catch (InterruptedException e) { + System.out.println("thread Interrupted."); + } + } } 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. - */ + for (int i = 1; i < tasks.size(); i++) { + int j = i; + while (j > 0 && tasks.get(j).processingTime > tasks.get(j - 1).processingTime) { + Task save = tasks.get(j); + tasks.set(j, tasks.get(j - 1)); + tasks.set(j - 1, save); + j--; + } + } + for (Task t: tasks) { + finishedTasks.add(t.taskName); + } + for (Task i: tasks) { + Thread thread = new Thread(i); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + System.out.println("Interrupted."); + } + } return finishedTasks; } public static void main(String[] args) { - // Test your code here } }