Skip to content

Develop #7

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 4 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
113 changes: 113 additions & 0 deletions Theoretical Questions.md
Original file line number Diff line number Diff line change
@@ -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 this code will be something like this:

## Running in: Thread-0
## Back to: main



74 changes: 59 additions & 15 deletions src/main/java/sbu/cs/MatrixMultiplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sbu.cs;

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

public class MatrixMultiplication {
Expand All @@ -8,16 +10,36 @@ public class MatrixMultiplication {
public static class BlockMultiplier implements Runnable
{
List<List<Integer>> tempMatrixProduct;
public BlockMultiplier() {
// TODO
List<List<Integer>> MatrixA;
List<List<Integer>> MatrixB;
int startrow;
int endrow;
int sum;


public BlockMultiplier(List<List<Integer>> MatrixA, List<List<Integer>> MatrixB, List<List<Integer>> tempMatrixProduct, int startrow, int endrow) {
this.MatrixA = MatrixA;
this.MatrixB = MatrixB;
this.tempMatrixProduct = tempMatrixProduct;
this.startrow = startrow;
this.endrow = endrow;
}

@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);
}
}
}
}

Expand All @@ -26,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<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;
public static List<List<Integer>> ParallelizeMatMul(List<List<Integer>> MatrixA, List<List<Integer>> MatrixB) {
int p = MatrixA.size();
int r = MatrixB.get(0).size();

List<List<Integer>> 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) {
Expand Down
56 changes: 44 additions & 12 deletions src/main/java/sbu/cs/TaskScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> doTasks(ArrayList<Task> tasks)
{
ArrayList<String> finishedTasks = new ArrayList<>();
ArrayList<Task> 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<Task> 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<String> finishedTasks = doTasks(tasks);

for (String task : finishedTasks) {
System.out.println(task);
}
}
}