From 78a88f6e4a782856c1cf12e6755a68f557b1baef Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Fri, 10 May 2024 11:40:59 +0330 Subject: [PATCH 01/11] sorting the tasks array list --- src/main/java/sbu/cs/TaskScheduler.java | 27 +++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/sbu/cs/TaskScheduler.java b/src/main/java/sbu/cs/TaskScheduler.java index 8725c2a..c0eeed9 100644 --- a/src/main/java/sbu/cs/TaskScheduler.java +++ b/src/main/java/sbu/cs/TaskScheduler.java @@ -1,6 +1,8 @@ package sbu.cs; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; public class TaskScheduler @@ -9,7 +11,7 @@ public static class Task implements Runnable { /* ------------------------- You don't need to modify this part of the code ------------------------- - */ + */ String taskName; int processingTime; @@ -17,12 +19,16 @@ public Task(String taskName, int processingTime) { this.taskName = taskName; this.processingTime = processingTime; } + public int getProcessingTime() { + return 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 @@ -33,7 +39,7 @@ public void run() { public static ArrayList doTasks(ArrayList tasks) { ArrayList finishedTasks = new ArrayList<>(); - + Collections.sort(tasks , Comparator.comparing(Task::getProcessingTime)); // for sorting the objects base on their processing time /* TODO Create a thread for each given task, And then start them based on which task has the highest priority @@ -41,11 +47,24 @@ public static ArrayList doTasks(ArrayList tasks) 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 = tasks.size()-1 ; i >= 0 ; i--) { // coping the tasks array list in the finishedTasks array list + finishedTasks.add(tasks.get(i).taskName); + } return finishedTasks; } public static void main(String[] args) { // Test your code here + ArrayList tasks = new ArrayList<>(); + + tasks.add(new Task("A", 100)); + tasks.add(new Task("B", 150)); + tasks.add(new Task("C", 200)); + tasks.add(new Task("E", 130)); + tasks.add(new Task("F", 300)); + + // for (int i = 0 ; i < tasks.size() ; i++ ) { + System.out.println(doTasks(tasks)); + // } } } From c44c9ad21e6dc1fd296e9d32adabe11f9d24bf0b Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Sat, 11 May 2024 21:38:42 +0330 Subject: [PATCH 02/11] tell the threads to do their tasks and wait for the next thread base on the processing time then run their tasks --- src/main/java/sbu/cs/TaskScheduler.java | 34 +++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/sbu/cs/TaskScheduler.java b/src/main/java/sbu/cs/TaskScheduler.java index c0eeed9..86cafc0 100644 --- a/src/main/java/sbu/cs/TaskScheduler.java +++ b/src/main/java/sbu/cs/TaskScheduler.java @@ -1,5 +1,7 @@ package sbu.cs; +import com.sun.source.tree.TryTree; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -33,11 +35,17 @@ public void run() { TODO Simulate utilizing CPU by sleeping the thread for the specified processingTime */ + + try{ + System.out.println(taskName + " is started "); + Thread.sleep(processingTime); + }catch(Exception e) { + System.out.println(e.getMessage()); + } } } - public static ArrayList doTasks(ArrayList tasks) - { + public static ArrayList doTasks(ArrayList tasks) throws InterruptedException { ArrayList finishedTasks = new ArrayList<>(); Collections.sort(tasks , Comparator.comparing(Task::getProcessingTime)); // for sorting the objects base on their processing time /* @@ -47,21 +55,31 @@ public static ArrayList doTasks(ArrayList tasks) 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. */ + Thread thread; + for (int i = 0 ; i < tasks.size() ; i++) { + thread = new Thread(tasks.get(i)); + thread.start(); + try { + thread.join(); + }catch (Exception e){ + e.printStackTrace(); + } + } for (int i = tasks.size()-1 ; i >= 0 ; i--) { // coping the tasks array list in the finishedTasks array list finishedTasks.add(tasks.get(i).taskName); } return finishedTasks; } - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { // Test your code here ArrayList tasks = new ArrayList<>(); - tasks.add(new Task("A", 100)); - tasks.add(new Task("B", 150)); - tasks.add(new Task("C", 200)); - tasks.add(new Task("E", 130)); - tasks.add(new Task("F", 300)); + tasks.add(new Task("A", 1000)); + tasks.add(new Task("B", 1200)); + tasks.add(new Task("C", 5000)); + tasks.add(new Task("E", 2000)); + tasks.add(new Task("F", 3000)); // for (int i = 0 ; i < tasks.size() ; i++ ) { System.out.println(doTasks(tasks)); From 4a65599934bdcc6ff95c657be39b992966ecc887 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Sat, 11 May 2024 21:40:04 +0330 Subject: [PATCH 03/11] complete the code of multiplying the two matrices with each other and then put the result into a List of List --- .../java/sbu/cs/MatrixMultiplication.java | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index 2f00c59..1471064 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,10 +9,13 @@ public class MatrixMultiplication { public static class BlockMultiplier implements Runnable { List> tempMatrixProduct; - public BlockMultiplier() { + List> matrix_A; + List> matrix_B; + public BlockMultiplier(List> matrix_A, List> matrix_B) { //constructor // TODO + this.matrix_A = matrix_A; + this.matrix_B = matrix_B; } - @Override public void run() { /* @@ -19,6 +23,26 @@ public void run() { Perform the calculation and store the final values in tempMatrixProduct */ } + public List> multiplyngMatrix(List> matrix_A, List> matrix_B) { + int rows1 = matrix_A.size(); + int cols1 = matrix_A.get(0).size(); + int cols2 = matrix_B.get(0).size(); + + List> result = new ArrayList<>(); + + for (int i = 0; i < rows1; i++) { + List row = new ArrayList<>(); + for (int j = 0; j < cols2; j++) { + int sum = 0; + for (int k = 0; k < cols1; k++) { + sum += matrix_A.get(i).get(k) * matrix_B.get(k).get(j); + } + row.add(sum); + } + result.add(row); + } + return result; + } } /* @@ -34,10 +58,24 @@ public static List> ParallelizeMatMul(List> matrix_A 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. */ + for (int i = 0 ; i < matrix_A.size()/2 ; i++) { + for (int j = 0 ; j < matrix_B.size() ; j++) { + + } + } + BlockMultiplier blockMultiplier1 = new BlockMultiplier(matrix_A , matrix_B); + Thread thread1 = new Thread(blockMultiplier1); + BlockMultiplier blockMultiplier2 = new BlockMultiplier(matrix_A , matrix_B); + Thread thread2 = new Thread(blockMultiplier1); + BlockMultiplier blockMultiplier3 = new BlockMultiplier(matrix_A , matrix_B); + Thread thread3 = new Thread(blockMultiplier1); + BlockMultiplier blockMultiplier4 = new BlockMultiplier(matrix_A , matrix_B); + Thread thread4 = new Thread(blockMultiplier1); return null; } public static void main(String[] args) { // Test your code here + } } From a120997132ebd3a3290b80ef5c82f79e024e6f54 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Sun, 12 May 2024 13:59:35 +0330 Subject: [PATCH 04/11] cut the matrix a , b to make 4 seperated matrices and pass each of them to one thread --- .../java/sbu/cs/MatrixMultiplication.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index 1471064..efbff29 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -22,8 +22,29 @@ public void run() { TODO Perform the calculation and store the final values in tempMatrixProduct */ + List> result1 = new ArrayList<>(); + for (int i = 0 ; i < matrix_A.size()/2 ; i++) { + for (int j = 0 ; j < matrix_B.get(i).size()/2 ; j++) { + result1.add(matrix_A.get(i)); + } + } + for (int i = 0 ; i < matrix_A.size()/2 ; i++) { + for (int j = matrix_B.get(i).size()/2 ; j < matrix_B.get(i).size() ; j++) { + + } + } + for (int i = matrix_A.size()/2 ; i < matrix_A.size() ; i++) { + for (int j = 0 ; j < matrix_B.get(i).size()/2 ; j++) { + + } + } + for (int i = matrix_A.size()/2 ; i < matrix_A.size() ; i++) { + for (int j = matrix_B.get(i).size() ; j < matrix_B.get(i).size()/2 ; j++) { + + } + } } - public List> multiplyngMatrix(List> matrix_A, List> matrix_B) { + public List> multiplyingMatrix(List> matrix_A, List> matrix_B) { //this method will multiply the matrices int rows1 = matrix_A.size(); int cols1 = matrix_A.get(0).size(); int cols2 = matrix_B.get(0).size(); @@ -58,11 +79,6 @@ public static List> ParallelizeMatMul(List> matrix_A 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. */ - for (int i = 0 ; i < matrix_A.size()/2 ; i++) { - for (int j = 0 ; j < matrix_B.size() ; j++) { - - } - } BlockMultiplier blockMultiplier1 = new BlockMultiplier(matrix_A , matrix_B); Thread thread1 = new Thread(blockMultiplier1); BlockMultiplier blockMultiplier2 = new BlockMultiplier(matrix_A , matrix_B); From cc2d6939fc72ced4798d09d7a7d8ec3ccac48ad2 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Mon, 13 May 2024 23:40:48 +0330 Subject: [PATCH 05/11] start the threads to ben started(means multiplying the matrices) --- .../java/sbu/cs/MatrixMultiplication.java | 155 ++++++++++++++---- 1 file changed, 123 insertions(+), 32 deletions(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index efbff29..5b775fa 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -1,5 +1,7 @@ package sbu.cs; +import javax.swing.plaf.TableHeaderUI; +import java.beans.PropertyEditorSupport; import java.util.ArrayList; import java.util.List; @@ -17,32 +19,12 @@ public BlockMultiplier(List> matrix_A, List> matrix_ this.matrix_B = matrix_B; } @Override - public void run() { + public void run() { // the only task of threads is to multiplying those matrices /* TODO Perform the calculation and store the final values in tempMatrixProduct */ - List> result1 = new ArrayList<>(); - for (int i = 0 ; i < matrix_A.size()/2 ; i++) { - for (int j = 0 ; j < matrix_B.get(i).size()/2 ; j++) { - result1.add(matrix_A.get(i)); - } - } - for (int i = 0 ; i < matrix_A.size()/2 ; i++) { - for (int j = matrix_B.get(i).size()/2 ; j < matrix_B.get(i).size() ; j++) { - - } - } - for (int i = matrix_A.size()/2 ; i < matrix_A.size() ; i++) { - for (int j = 0 ; j < matrix_B.get(i).size()/2 ; j++) { - - } - } - for (int i = matrix_A.size()/2 ; i < matrix_A.size() ; i++) { - for (int j = matrix_B.get(i).size() ; j < matrix_B.get(i).size()/2 ; j++) { - - } - } + multiplyingMatrix(matrix_A , matrix_B); } public List> multiplyingMatrix(List> matrix_A, List> matrix_B) { //this method will multiply the matrices int rows1 = matrix_A.size(); @@ -62,10 +44,34 @@ public List> multiplyingMatrix(List> matrix_A, List< } result.add(row); } + tempMatrixProduct = result; return result; } } + // ---------------------------------- combining the four matrices(blocks) together ------------------------------------------ + public List> khodeMatrix (List> block1 , List> block2 , List> block3 , List> block4) { + // block 1 : up & left + // block 2 : up & right + // block 3 : down & left + // block 4 : down & right + // first we combine upper blocks then lower block with each other, then combine them to a final matrix + List> finalMatrix = new ArrayList<>(); + // combining block 1 & block 2 (upper blocks) + for (int i = 0 ; i < block1.size() ; i++) { + List row = new ArrayList<>(block1.get(i)); // copying the row of the block1 + row.addAll(block2.get(i)); + finalMatrix.add(row); + } + // combining block 3 & block 4 (lower blocks) + for (int i = 0 ; i < block3.size() ; i++) { + List row = new ArrayList<>(block3.get(i)); // copying the row of the block1 + row.addAll(block4.get(i)); + finalMatrix.add(row); + } + return finalMatrix; + } + // ---------------------------------- combining the four matrices(blocks) together ------------------------------------------ /* Matrix A is of the form p x q Matrix B is of the form q x r @@ -79,19 +85,104 @@ public static List> ParallelizeMatMul(List> matrix_A 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. */ - BlockMultiplier blockMultiplier1 = new BlockMultiplier(matrix_A , matrix_B); - Thread thread1 = new Thread(blockMultiplier1); - BlockMultiplier blockMultiplier2 = new BlockMultiplier(matrix_A , matrix_B); - Thread thread2 = new Thread(blockMultiplier1); - BlockMultiplier blockMultiplier3 = new BlockMultiplier(matrix_A , matrix_B); - Thread thread3 = new Thread(blockMultiplier1); - BlockMultiplier blockMultiplier4 = new BlockMultiplier(matrix_A , matrix_B); - Thread thread4 = new Thread(blockMultiplier1); - return null; + List> temp1 = new ArrayList<>(); + List> temp2 = new ArrayList<>(); + List> temp3 = new ArrayList<>(); + List> temp4 = new ArrayList<>(); + + //----------------------------------------------for separating the matrix--------------------------------------------------- + + for (int i = 0; i < matrix_A.size()/2; i++) { // the upper part of the matrix ---> thread 1 + temp1.add(matrix_A.get(i)); + } + + for (int i = 0; i < matrix_B.size(); i++) { // the left part of the matrix ---> thread 2 + List row = new ArrayList<>(); + for (int j = 0; j < matrix_B.get(0).size()/2 ; j++) { + row.add(matrix_B.get(i).get(j)); + } + temp2.add(row); + } + + for (int i = matrix_A.size()/2 ; i < matrix_A.size(); i++) { // the down part of the matrix --->thread 3 + temp3.add(matrix_A.get(i)); + } + + + for (int i = 0; i < matrix_B.size(); i++) { // the right part of the matrix ---> thread 4 + List row = new ArrayList<>(); + for (int j = matrix_B.get(0).size()/2 ; j < matrix_B.get(0).size() ; j++) { + row.add(matrix_B.get(i).get(j)); + } + temp4.add(row); + } + // ----------------------------------------------for separating the matrix--------------------------------------------------- + + List listOfBlocks = new ArrayList<>(); + List listOfThreads = new ArrayList<>(); + + + BlockMultiplier blockMultiplier1 = new BlockMultiplier(temp1 , temp2); //up & left + listOfBlocks.add(blockMultiplier1); + + BlockMultiplier blockMultiplier2 = new BlockMultiplier(temp1 , temp4); //up & right + listOfBlocks.add(blockMultiplier2); + + BlockMultiplier blockMultiplier3 = new BlockMultiplier(temp3 , temp2); // down & left + listOfBlocks.add(blockMultiplier3); + + BlockMultiplier blockMultiplier4 = new BlockMultiplier(temp3 , temp4); //down & right + listOfBlocks.add(blockMultiplier4); + // ------------------------------------------------------------------------------------------------------------------------- + // ----------------------------------------------for starting the threads--------------------------------------------------- + for (int i = 0 ; i < listOfBlocks.size() ; i++) { + Thread thread = new Thread(listOfBlocks.get(i)); + listOfThreads.add(thread); + } + + for (int i = 0 ; i < listOfThreads.size() ; i++) { + listOfThreads.get(i).start(); + + } + // ----------------------------------------------for starting the threads--------------------------------------------------- + // ------------------------------------------------------------------------------------------------------------------------- + // ---------------------------------- combining the four matrices(blocks) together ------------------------------------------ + MatrixMultiplication matrixMultiplication = new MatrixMultiplication(); + // ---------------------------------- combining the four matrices(blocks) together ------------------------------------------ + // ------------------------------------------------------------------------------------------------------------------------- + return matrixMultiplication.khodeMatrix(blockMultiplier1.tempMatrixProduct , blockMultiplier2.tempMatrixProduct , blockMultiplier3.tempMatrixProduct , blockMultiplier4.tempMatrixProduct); } public static void main(String[] args) { // Test your code here - + List> matrix_A = new ArrayList<>(); + List> matrix_B = new ArrayList<>(); + List row1A = new ArrayList<>(); + row1A.add(1); + row1A.add(2); + row1A.add(3); + List row2A = new ArrayList<>(); + row1A.add(4); + row1A.add(5); + row1A.add(6); + List row3A = new ArrayList<>(); + row1A.add(7); + row1A.add(8); + row1A.add(9); + List row4A = new ArrayList<>(); + row1A.add(10); + row1A.add(11); + row1A.add(12); + List row1B = new ArrayList<>(); + row1A.add(13); + row1A.add(14); + List row2B = new ArrayList<>(); + row1A.add(15); + row1A.add(16); + List row3B = new ArrayList<>(); + row1A.add(17); + row1A.add(18); + BlockMultiplier blockMultiplier = new BlockMultiplier(matrix_A , matrix_B); + blockMultiplier.multiplyingMatrix(matrix_A , matrix_B); } } From 8b0b778d3bc742dfd9810a88f10d37d2225b1942 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Mon, 13 May 2024 23:42:30 +0330 Subject: [PATCH 06/11] combining the four block together to make the final matrix which made of those four block(the four block which any one of them is made by one thread) --- src/main/java/sbu/cs/MatrixMultiplication.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index 5b775fa..e5f7fdd 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -48,7 +48,6 @@ public List> multiplyingMatrix(List> matrix_A, List< return result; } } - // ---------------------------------- combining the four matrices(blocks) together ------------------------------------------ public List> khodeMatrix (List> block1 , List> block2 , List> block3 , List> block4) { // block 1 : up & left From 324eb598dfa26442c0f0a1c05530ffe316b7d919 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Mon, 13 May 2024 23:43:53 +0330 Subject: [PATCH 07/11] at the end test the code in the main method --- src/main/java/sbu/cs/MatrixMultiplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index e5f7fdd..fb6908f 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -176,7 +176,7 @@ public static void main(String[] args) { row1A.add(13); row1A.add(14); List row2B = new ArrayList<>(); - row1A.add(15); + row1A.add(17); row1A.add(16); List row3B = new ArrayList<>(); row1A.add(17); From 0f050eb6f83e5c9f507e4d6931a3466d1f8fe6af Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Mon, 13 May 2024 23:46:24 +0330 Subject: [PATCH 08/11] answering the first code of theory questions --- src/main/java/sbu/cs/Theory-Questions.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/sbu/cs/Theory-Questions.md diff --git a/src/main/java/sbu/cs/Theory-Questions.md b/src/main/java/sbu/cs/Theory-Questions.md new file mode 100644 index 0000000..1fa356b --- /dev/null +++ b/src/main/java/sbu/cs/Theory-Questions.md @@ -0,0 +1,30 @@ +### First Code : +``` + +public static class SleepThread extends Thread { + public void run() { + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + System.out.println("Thread was interrupted!"); + } finally { + System.out.println("Thread will be finished here!!!"); + } + } + } + + public static void main(String[] args) { + SleepThread thread = new SleepThread(); + thread.start(); + thread.interrupt(); + } + +``` +### Output : +#### "Thread was interrupted!" +#### "Thread will be finished here!!!" + + +### Why : +#### in this provided code inside the main method we call the start method to start the threads, so it means the thread is trying to be run then we call the interrupt() method which will interrupt the function of the thread and it makes an exception, why ? because the thread is running then we interrupt its function so the output will be : "Thread was interrupted!" and then "Thread will be finished here!!!". + From 6ef306fb9af460a4e5a4f78e0b7e64d62ebccad6 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Mon, 13 May 2024 23:47:05 +0330 Subject: [PATCH 09/11] answering the second code of theory questions --- src/main/java/sbu/cs/Theory-Questions.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/sbu/cs/Theory-Questions.md b/src/main/java/sbu/cs/Theory-Questions.md index 1fa356b..326ebd4 100644 --- a/src/main/java/sbu/cs/Theory-Questions.md +++ b/src/main/java/sbu/cs/Theory-Questions.md @@ -27,4 +27,24 @@ public static class SleepThread extends Thread { ### Why : #### in this provided code inside the main method we call the start method to start the threads, so it means the thread is trying to be run then we call the interrupt() method which will interrupt the function of the thread and it makes an exception, why ? because the thread is running then we interrupt its function so the output will be : "Thread was interrupted!" and then "Thread will be finished here!!!". +### Second Code : +``` +public class DirectRunnable implements Runnable { + public void run() { + System.out.println("Running in: " + Thread.currentThread().getName()); + } +} + +public class Main { + public static void main(String[] args) { + DirectRunnable runnable = new DirectRunnable(); + runnable.run(); + } +} +``` + +### Output : +#### Running in: main +### Why : +#### when we do not initialize a new Thread it will consider the default thread means main thread then the current thread will be main thread From be925e601d1a03ca5de79ca6abf534b78b76ae57 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Mon, 13 May 2024 23:47:38 +0330 Subject: [PATCH 10/11] answering the third code of theory questions --- src/main/java/sbu/cs/Theory-Questions.md | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/sbu/cs/Theory-Questions.md b/src/main/java/sbu/cs/Theory-Questions.md index 326ebd4..cdde720 100644 --- a/src/main/java/sbu/cs/Theory-Questions.md +++ b/src/main/java/sbu/cs/Theory-Questions.md @@ -47,4 +47,30 @@ public class Main { #### Running in: main ### Why : #### when we do not initialize a new Thread it will consider the default thread means main thread then the current thread will be main thread +### Third Code : +``` +public class JoinThread extends Thread { + public void run() { + System.out.println("Running in: " + Thread.currentThread().getName()); + } +} + +public class Main { + public static void main(String[] args) { + JoinThread thread = new JoinThread(); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Back to: " + Thread.currentThread().getName()); + } +} +``` +### Answer : +#### because the join() method causes this event : the calling thread to wait for the specified thread to be finished : +#### 1. we are taking an object from that class which will run the thread +#### 2. we call start() method for that thread +#### 3. then we are calling the join() method for that thread which is meaning this that the calling thread will wait for the specific thread to be ran and executed then it will continue which in this code the Mian thread will wait for the object thread to be executed then the Main thread will continue**** From 02e46bd6b5c824a7cfbda2fffc9a5447117e7246 Mon Sep 17 00:00:00 2001 From: Fateme Lashkari Date: Tue, 21 May 2024 18:08:11 +0330 Subject: [PATCH 11/11] debugging thread part, which it has an error --- .../java/sbu/cs/MatrixMultiplication.java | 90 ++++++++++++------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index fb6908f..c67c9d9 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -24,9 +24,9 @@ public void run() { // the only task of threads is to multiplying those matrices TODO Perform the calculation and store the final values in tempMatrixProduct */ - multiplyingMatrix(matrix_A , matrix_B); + tempMatrixProduct = multiplyingMatrix(matrix_A , matrix_B); } - public List> multiplyingMatrix(List> matrix_A, List> matrix_B) { //this method will multiply the matrices + public List> multiplyingMatrix(List> matrix_A, List> matrix_B) { //this method will multiply that two matrices which we pass it int rows1 = matrix_A.size(); int cols1 = matrix_A.get(0).size(); int cols2 = matrix_B.get(0).size(); @@ -58,13 +58,13 @@ public List> khodeMatrix (List> block1 , List> finalMatrix = new ArrayList<>(); // combining block 1 & block 2 (upper blocks) for (int i = 0 ; i < block1.size() ; i++) { - List row = new ArrayList<>(block1.get(i)); // copying the row of the block1 + List row = new ArrayList<>(block1.get(i)); row.addAll(block2.get(i)); finalMatrix.add(row); } // combining block 3 & block 4 (lower blocks) for (int i = 0 ; i < block3.size() ; i++) { - List row = new ArrayList<>(block3.get(i)); // copying the row of the block1 + List row = new ArrayList<>(block3.get(i)); row.addAll(block4.get(i)); finalMatrix.add(row); } @@ -78,12 +78,6 @@ public List> khodeMatrix (List> block1 , 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. - */ List> temp1 = new ArrayList<>(); List> temp2 = new ArrayList<>(); List> temp3 = new ArrayList<>(); @@ -107,7 +101,6 @@ public static List> ParallelizeMatMul(List> matrix_A temp3.add(matrix_A.get(i)); } - for (int i = 0; i < matrix_B.size(); i++) { // the right part of the matrix ---> thread 4 List row = new ArrayList<>(); for (int j = matrix_B.get(0).size()/2 ; j < matrix_B.get(0).size() ; j++) { @@ -115,11 +108,10 @@ public static List> ParallelizeMatMul(List> matrix_A } temp4.add(row); } + // ----------------------------------------------for separating the matrix--------------------------------------------------- List listOfBlocks = new ArrayList<>(); - List listOfThreads = new ArrayList<>(); - BlockMultiplier blockMultiplier1 = new BlockMultiplier(temp1 , temp2); //up & left listOfBlocks.add(blockMultiplier1); @@ -132,24 +124,40 @@ public static List> ParallelizeMatMul(List> matrix_A BlockMultiplier blockMultiplier4 = new BlockMultiplier(temp3 , temp4); //down & right listOfBlocks.add(blockMultiplier4); + // ------------------------------------------------------------------------------------------------------------------------- + // ----------------------------------------------for starting the threads--------------------------------------------------- - for (int i = 0 ; i < listOfBlocks.size() ; i++) { - Thread thread = new Thread(listOfBlocks.get(i)); + + List listOfThreads = new ArrayList<>(); + for (BlockMultiplier blockMultiplier : listOfBlocks) { + Thread thread = new Thread(blockMultiplier); listOfThreads.add(thread); } - for (int i = 0 ; i < listOfThreads.size() ; i++) { - listOfThreads.get(i).start(); + for (Thread listOfThread : listOfThreads) { + listOfThread.start(); + } + for (Thread thread : listOfThreads) { + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } + // ----------------------------------------------for starting the threads--------------------------------------------------- // ------------------------------------------------------------------------------------------------------------------------- // ---------------------------------- combining the four matrices(blocks) together ------------------------------------------ + MatrixMultiplication matrixMultiplication = new MatrixMultiplication(); + // ---------------------------------- combining the four matrices(blocks) together ------------------------------------------ // ------------------------------------------------------------------------------------------------------------------------- - return matrixMultiplication.khodeMatrix(blockMultiplier1.tempMatrixProduct , blockMultiplier2.tempMatrixProduct , blockMultiplier3.tempMatrixProduct , blockMultiplier4.tempMatrixProduct); + List> matrix = matrixMultiplication.khodeMatrix(blockMultiplier1.tempMatrixProduct , + blockMultiplier2.tempMatrixProduct , blockMultiplier3.tempMatrixProduct , blockMultiplier4.tempMatrixProduct); + return matrix; } public static void main(String[] args) { @@ -161,27 +169,41 @@ public static void main(String[] args) { row1A.add(2); row1A.add(3); List row2A = new ArrayList<>(); - row1A.add(4); - row1A.add(5); - row1A.add(6); + row2A.add(4); + row2A.add(5); + row2A.add(6); List row3A = new ArrayList<>(); - row1A.add(7); - row1A.add(8); - row1A.add(9); + row3A.add(7); + row3A.add(8); + row3A.add(9); List row4A = new ArrayList<>(); - row1A.add(10); - row1A.add(11); - row1A.add(12); + row4A.add(10); + row4A.add(11); + row4A.add(12); + matrix_A.add(row1A); + matrix_A.add(row2A); + matrix_A.add(row3A); + matrix_A.add(row4A); List row1B = new ArrayList<>(); - row1A.add(13); - row1A.add(14); + row1B.add(13); + row1B.add(14); List row2B = new ArrayList<>(); - row1A.add(17); - row1A.add(16); + row2B.add(17); + row2B.add(16); List row3B = new ArrayList<>(); - row1A.add(17); - row1A.add(18); + row3B.add(17); + row3B.add(18); + matrix_B.add(row1B); + matrix_B.add(row2B); + matrix_B.add(row3B); BlockMultiplier blockMultiplier = new BlockMultiplier(matrix_A , matrix_B); - blockMultiplier.multiplyingMatrix(matrix_A , matrix_B); + List> result; + result = blockMultiplier.multiplyingMatrix(matrix_A , matrix_B); + for (List innerList : result) { + for (Integer element : innerList) { + System.out.print(element + " "); + } + System.out.println(); + } } }