Skip to content

Develo #5

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 3 commits 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
29 changes: 29 additions & 0 deletions answer.md
Original file line number Diff line number Diff line change
@@ -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

97 changes: 83 additions & 14 deletions src/main/java/sbu/cs/MatrixMultiplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sbu.cs;

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

public class MatrixMultiplication {
Expand All @@ -8,16 +9,40 @@ public class MatrixMultiplication {
public static class BlockMultiplier implements Runnable
{
List<List<Integer>> tempMatrixProduct;
public BlockMultiplier() {
// TODO
int num;
List<List<Integer>> matrix_A, matrix_B;
public BlockMultiplier(int num, List<List<Integer>> matrix_A, List<List<Integer>> 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<Integer> 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<List<Integer>> getTempMatrixProduct() {
return tempMatrixProduct;
}
}

Expand All @@ -28,16 +53,60 @@ public void run() {
*/
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;
List<List<Integer>> ParallelizeMatMul = new ArrayList<>();

List<List<Integer>> mat1 = new ArrayList<>();
List<List<Integer>> mat2 = new ArrayList<>();
List<List<Integer>> mat3 = new ArrayList<>();
List<List<Integer>> 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<Integer> 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<Integer> 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

}
}
42 changes: 28 additions & 14 deletions src/main/java/sbu/cs/TaskScheduler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sbu.cs;

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

public class TaskScheduler
Expand All @@ -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;
Expand All @@ -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<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.
*/
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
}
}