Skip to content

Develop #6

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
28 changes: 28 additions & 0 deletions Answer
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## First Question:
1.An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.
2.In this code the target thread has been sleeping (by calling sleep()) , it will be interrupted , meaning that it stops waiting for what was waiting for and receives an InterruptedException instead.

## Output:
The outputs of this code are : "
Thread was interrupted!
Thread will be finished here!!


## Second Question:
1.In this code the run() method directly on a Runnable object in Java will not start a new thread. Instead, the run() method will execute in the current thread, just like any other method call. This means that the code within the run() method will run sequentially in the calling thread, and no multithreading behavior will occur.
The run() method is called directly, so the message will be printed from the main thread.

## Output:
The output of this code is :
Runnig in : main


## Third Question:
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:
The outputs of this code are:
Running in: Thread-0
Back to: main


150 changes: 0 additions & 150 deletions README.md

This file was deleted.

134 changes: 128 additions & 6 deletions src/main/java/sbu/cs/MatrixMultiplication.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package sbu.cs;

import java.sql.SQLOutput;
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
{
List<List<Integer>> tempMatrixProduct;
public BlockMultiplier() {
public static class BlockMultiplier implements Runnable {
List<List<Integer>> tempMatrixProduct = new ArrayList<>();
int task;
List<List<Integer>> matrix_A, matrix_B;

public BlockMultiplier(List<List<Integer>> matrix_A, List<List<Integer>> matrix_B, int task) {
// TODO
this.task = task;
this.matrix_A = matrix_A;
this.matrix_B = matrix_B;
}

@Override
Expand All @@ -18,6 +25,70 @@ public void run() {
TODO
Perform the calculation and store the final values in tempMatrixProduct
*/
int m ,n,r;
m = matrix_A.size();
r = matrix_B.get(0).size();
n = matrix_B.size();
if (task == 1 ){
for (int i =0 ; i<m/2 ; i++){
List<Integer> satr = new ArrayList<>();
for (int j =0 ; j<r/2 ; j++){
int sum =0 ;
for (int k = 0 ; k<n ; k++){
sum+=matrix_A.get(i).get(k)*matrix_B.get(k).get(j);
}
satr.add(sum);
}
tempMatrixProduct.add(satr);
}
}

else if (task ==2){
for (int i =0 ; i<m/2 ; i++){
List<Integer> satr = new ArrayList<>();
for (int j =r/2 ; j<r ; j++){
int sum =0 ;
for (int k = 0 ; k<n ; k++){
sum+=matrix_A.get(i).get(k)*matrix_B.get(k).get(j);
}
satr.add(sum);
}
tempMatrixProduct.add(satr);
}
}

else if (task ==3){

for (int i =m/2 ; i<m ; i++){
List<Integer> satr = new ArrayList<>();
for (int j =0 ; j<r/2 ; j++){
int sum =0 ;
for (int k = 0 ; k<n ; k++){
sum+=matrix_A.get(i).get(k)*matrix_B.get(k).get(j);
}
satr.add(sum);
}
tempMatrixProduct.add(satr);
}

}

else if (task ==4){
for (int i =m/2 ; i<m ; i++){
List<Integer> satr = new ArrayList<>();
for (int j =r/2 ; j<r ; j++){
int sum =0 ;
for (int k = 0 ; k<n ; k++){
sum+=matrix_A.get(i).get(k)*matrix_B.get(k).get(j);
}
satr.add(sum);
}
tempMatrixProduct.add(satr);
}
}
}
public List<List<Integer>> getTempMatrixProduct() {
return tempMatrixProduct;
}
}

Expand All @@ -34,10 +105,61 @@ public static List<List<Integer>> ParallelizeMatMul(List<List<Integer>> 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.
*/
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(matrix_A, matrix_B, 1);
BlockMultiplier BM2 = new BlockMultiplier( matrix_A, matrix_B,2);
BlockMultiplier BM3 = new BlockMultiplier( matrix_A, matrix_B,3);
BlockMultiplier BM4 = new BlockMultiplier( matrix_A, matrix_B , 4);

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

}
}
Loading