From f728b2942188d5043b767d12234a1f70e6f14ca3 Mon Sep 17 00:00:00 2001 From: zahramoghaddasi Date: Fri, 10 May 2024 18:22:23 +0330 Subject: [PATCH 1/4] Basic information --- src/main/java/sbu/cs/MatrixMultiplication.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index 2f00c59..e9a3012 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -8,8 +8,18 @@ public class MatrixMultiplication { public static class BlockMultiplier implements Runnable { List> tempMatrixProduct; - public BlockMultiplier() { - // TODO + List> MatrixA; + List> MatrixB; + int startrow; + int endrow; + + + public BlockMultiplier(List> MatrixA, List> MatrixB, List> tempMatrixProduct, int startrow, int endrow) { + this.MatrixA = MatrixA; + this.MatrixB = MatrixB; + this.tempMatrixProduct = tempMatrixProduct; + this.startrow = startrow; + this.endrow = endrow; } @Override From f473f3180513929821e4f4b9118104d261058d73 Mon Sep 17 00:00:00 2001 From: zahramoghaddasi Date: Fri, 10 May 2024 18:52:07 +0330 Subject: [PATCH 2/4] Final matrix calculations --- src/main/java/sbu/cs/MatrixMultiplication.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index e9a3012..7192174 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -12,6 +12,7 @@ public static class BlockMultiplier implements Runnable List> MatrixB; int startrow; int endrow; + int sum; public BlockMultiplier(List> MatrixA, List> MatrixB, List> tempMatrixProduct, int startrow, int endrow) { @@ -24,10 +25,19 @@ public BlockMultiplier(List> MatrixA, List> MatrixB, @Override public void run() { - /* - TODO - Perform the calculation and store the final values in tempMatrixProduct - */ + int p = MatrixA.size(); //تعداد سطر های ماتریس اول + int r = MatrixB.get(0).size(); //تعداد ستون های ماتریس دوم + int q = MatrixA.get(0).size(); //تعداد ستون های ماتری اول که همان تعداد سطر های ماتریس دوم است + + for(int i = startrow ; i < endrow ; i++){ + for(int j = 0 ; j < r ; j++){ + sum = 0; + for(int m = 0 ; m < q ; m++){ + sum += MatrixA.get(i).get(m) * MatrixB.get(m).get(j); + } + tempMatrixProduct.get(i).set(j , sum); + } + } } } From 92e7f95265d371d3b808ee071ce4b3b7c7c57642 Mon Sep 17 00:00:00 2001 From: zahramoghaddasi Date: Fri, 10 May 2024 21:45:27 +0330 Subject: [PATCH 3/4] Finally --- Theoretical Questions.md | 113 ++++++++++++++++++ .../java/sbu/cs/MatrixMultiplication.java | 42 +++++-- src/main/java/sbu/cs/TaskScheduler.java | 56 +++++++-- 3 files changed, 190 insertions(+), 21 deletions(-) create mode 100644 Theoretical Questions.md diff --git a/Theoretical Questions.md b/Theoretical Questions.md new file mode 100644 index 0000000..15a89c0 --- /dev/null +++ b/Theoretical Questions.md @@ -0,0 +1,113 @@ +# Theoretical Questions 📝 + + +## Questions 1 : + +- What will be printed after interrupting the thread? + +## Code in java +## [ +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(); + } +## ] + +## answer : +When the interrupt() method is called on a thread, it sets the interrupt flag of the thread to true. If the thread is currently in a sleeping or waiting state (which it is in this case due to the Thread.sleep(10000);), An InterruptedException will be thrown and the interrupt flag of the thread is cleared (set to false). + In this code, when the thread.interrupt(); is called in the main method, it interrupts the sleeping thread, causing it to An InterruptedException is thrown. This exception is caught in the catch block and “Thread was interrupted!” is printed. After that, the finally block is executed and “Thread will be finished here!!!” is printed. + +# So, the output of this code will be: + +## Thread was interrupted! +## Thread will be finished here!!! +# ___________________________________________________________________________________________________________ + +## Questions 2 : + +- In Java, what would be the outcome if the run() method of a Runnable object is invoked directly, without initiating it inside a Thread object? + +## Code in java: +## [ + 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(); + } +} +## ] + +## answer: +In Java, if you invoke the run() method directly without initiating it inside a Thread object, it will not start a new thread. Instead, the run() method will be executed in the current thread, just like any other method call. +In this code, the run() method of the DirectRunnable object is called directly in the main method. Therefore, it will be executed in the main thread, not in a new thread. the Thread.currentThread().getName() method returns the name of the currently executing thread, which is the main thread in this case. + +# So, the output of this code will be: + +## Running in: main +# ___________________________________________________________________________________________________________ + +## Questions 3 : + +- Elaborate on the sequence of events that occur when the join() method of a thread (let's call it Thread_0) is invoked within the Main() method of a Java program. + +## Code in java: +## [ + 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: +In Java, the join() method is used to pause the current thread (in this case, the main thread) until the thread on which join() is called (in this case, Thread_0) finishes its execution. Here’s the sequence of events that occur when join() is invoked in your code: + +1. JoinThread thread = new JoinThread(); - A new JoinThread object named thread is created. + +2. thread.start(); - The start() method is called on thread, which causes a new thread of execution to start. The run() method of thread is called in this new thread. The System.out.println("Running in: " + Thread.currentThread().getName()); statement is executed in this new thread, so it prints “Running in: Thread-0”. + +3. thread.join(); - The main thread calls join() on thread. This causes the main thread to pause and wait for thread to finish its execution. If thread is interrupted while main is waiting, an InterruptedException will be thrown. + +4. Once thread has finished its execution, the main thread resumes. The System.out.println("Back to: " + Thread.currentThread().getName()); statement is executed in the main thread, so it prints “Back to: main”. + +5. If thread is interrupted during its execution, the InterruptedException is caught and the stack trace of the exception is printed. + +# So, the output of your code will be something like this: + +## 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 7192174..8e8afa2 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 java.util.ArrayList; +import java.util.Collections; import java.util.List; public class MatrixMultiplication { @@ -46,15 +48,37 @@ public void run() { 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> MatrixA, List> MatrixB) { + int p = MatrixA.size(); + int r = MatrixB.get(0).size(); + + List> MatrixProduct = new ArrayList<>(); + for (int i = 0; i < p; i++) { + MatrixProduct.add(new ArrayList<>(Collections.nCopies(r, 0))); //مقدار دهی اولیه برای ماتریس جدید + } + + Thread thread1 = new Thread(new BlockMultiplier(MatrixA, MatrixB, MatrixProduct, 0, (p/4))); + Thread thread2 = new Thread(new BlockMultiplier(MatrixA, MatrixB, MatrixProduct, (p/4), p/2)); + Thread thread3 = new Thread(new BlockMultiplier(MatrixA, MatrixB, MatrixProduct, p/2, ((3/4)*p))); + Thread thread4 = new Thread(new BlockMultiplier(MatrixA, MatrixB, MatrixProduct, ((3/4)*p), p)); + + + thread1.start(); + thread2.start(); + thread3.start(); + thread4.start(); + + try{ + thread1.join(); + thread2.join(); + thread3.join(); + thread4.join(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + + return MatrixProduct; } public static void main(String[] args) { diff --git a/src/main/java/sbu/cs/TaskScheduler.java b/src/main/java/sbu/cs/TaskScheduler.java index 8725c2a..2050b10 100644 --- a/src/main/java/sbu/cs/TaskScheduler.java +++ b/src/main/java/sbu/cs/TaskScheduler.java @@ -23,29 +23,61 @@ 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) { + e.printStackTrace(); + } } } public static ArrayList doTasks(ArrayList tasks) { ArrayList finishedTasks = new ArrayList<>(); + ArrayList sortedtask = 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 (Task task : tasks) { + boolean added = false; + for (int i = 0; i < sortedtask.size(); i++) { + if (task.processingTime >= sortedtask.get(i).processingTime) { + sortedtask.add(i, task); + added = true; + break; + } + } + if (!added) { + sortedtask.add(task); + } + } + for (Task task : sortedtask) { + Thread thread = new Thread(task); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + finishedTasks.add(task.taskName); + } return finishedTasks; } public static void main(String[] args) { - // Test your code here + ArrayList tasks = new ArrayList<>(); + + tasks.add(new Task("A", 200)); + tasks.add(new Task("B", 250)); + tasks.add(new Task("C", 150)); + tasks.add(new Task("E", 500)); + tasks.add(new Task("F", 50)); + tasks.add(new Task("G", 300)); + + ArrayList finishedTasks = doTasks(tasks); + + for (String task : finishedTasks) { + System.out.println(task); + } } } From ed49c505cd1366023328c043e17a2e8be9dcad62 Mon Sep 17 00:00:00 2001 From: zahramoghaddasi <160654770+zahramoghaddasi@users.noreply.github.com> Date: Fri, 10 May 2024 21:50:57 +0330 Subject: [PATCH 4/4] Update Theoretical Questions.md --- Theoretical Questions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Theoretical Questions.md b/Theoretical Questions.md index 15a89c0..f83a371 100644 --- a/Theoretical Questions.md +++ b/Theoretical Questions.md @@ -104,7 +104,7 @@ In Java, the join() method is used to pause the current thread (in this case, th 5. If thread is interrupted during its execution, the InterruptedException is caught and the stack trace of the exception is printed. -# So, the output of your code will be something like this: +# So, the output of this code will be something like this: ## Running in: Thread-0 ## Back to: main