Skip to content
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
Binary file added .gradle/7.4.2/checksums/checksums.lock
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file added .gradle/7.4.2/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/7.4.2/fileHashes/fileHashes.lock
Binary file not shown.
Empty file added .gradle/7.4.2/gc.properties
Empty file.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
4 changes: 2 additions & 2 deletions .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Tue Apr 25 19:43:33 GMT+03:30 2023
gradle.version=7.5.1
#Wed May 03 18:33:36 IRDT 2023
gradle.version=7.4.2
Binary file removed .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
25 changes: 25 additions & 0 deletions Report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 6th Assignment Report

![](https://github.com/kianaghamsari/Second-Assignment/blob/develop/uni.png)

## Kiana Ghamsari - 400222079


# Introduction

The purpose of this project is to review the concepts of multithreaded programming and utilize them correctly.


# Design and Implementation

The following exercises have been completed:
* `CPU Simulator`: The `sort()` function has been implemented to sort tasks based on their time and then a for loop is created to create, run, and wait for them to complete.
* `Find Multiples`: An array list of unique integers has been created to store multiples of 3, 5, and 7, each handled by 3 separate tasks. Then, the sum of them will be returned. The `synchronized` keyword is used to handle race conditions.
* `Use Interrupts`: The interruptions have been handled in each task, and, each thread is terminated after 3000 milliseconds using the `interrupt()` method.


# Conclusion

Threads allow a program to operate more efficiently by doing multiple things at the same time.
Multithreading allows concurrent execution of two or more parts of a program. In this case, handling race conditions is important. They occur when two threads operate on the same object without proper synchronization and their operation interleaves on each other.
An easy way to handle them is to use the `synchronized` keyword.
58 changes: 53 additions & 5 deletions src/main/java/sbu/cs/CPU_Simulator.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package sbu.cs;

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

/*
For this exercise, you must simulate a CPU with a single core.
Expand All @@ -22,8 +23,26 @@ public class CPU_Simulator
public static class Task implements Runnable {
long processingTime;
String ID;

public Task(String ID, long processingTime) {
// TODO
this.ID = ID;
this.processingTime = processingTime;
}

public long getProcessingTime() {
return processingTime;
}

public void setProcessingTime(long processingTime) {
this.processingTime = processingTime;
}

public String getID() {
return ID;
}

public void setID(String iD) {
ID = iD;
}

/*
Expand All @@ -32,7 +51,12 @@ public Task(String ID, long processingTime) {
*/
@Override
public void run() {
// TODO
try {
Thread.sleep(processingTime);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

Expand All @@ -44,11 +68,35 @@ Here the CPU selects the next shortest task to run (also known as the
public ArrayList<String> startSimulation(ArrayList<Task> tasks) {
ArrayList<String> executedTasks = new ArrayList<>();

// TODO
ArrayList<Task> sortedTasks = sort(tasks);
for (Task task: sortedTasks) {
Thread t = new Thread(task);
try {
t.start();
t.join();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}

executedTasks.add(task.getID());
}



return executedTasks;
}

public ArrayList<Task> sort(ArrayList<Task> tasks){
for(int i=0;i<tasks.size();i++){
for(int j=i+1;j<tasks.size();j++){
if(tasks.get(i).processingTime>tasks.get(j).processingTime){
Collections.swap(tasks,i,j);
}
}
}
return tasks;
}

public static void main(String[] args) {
}
}
49 changes: 47 additions & 2 deletions src/main/java/sbu/cs/FindMultiples.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sbu.cs;

import java.util.ArrayList;

/*
In this exercise, you must write a multithreaded program that finds all
integers in the range [1, n] that are divisible by 3, 5, or 7. Return the
Expand All @@ -24,15 +26,58 @@ public class FindMultiples

// TODO create the required multithreading class/classes using your preferred method.

private static int sum;
private static ArrayList<Integer> numbers;


public static class Divide implements Runnable {
private int n;
private int d;

public Divide(int n, int d) {
this.n = n;
this.d = d;
}

public static synchronized void sum(int number) {
if (!numbers.contains(number)) {
sum += number;
numbers.add(number);
}
}

@Override
public void run() {
for(int i = d; i <= n; i += d) {
sum(i);
}
}
}

/*
The getSum function should be called at the start of your program.
New Threads and tasks should be created here.
*/


public int getSum(int n) {
int sum = 0;
numbers = new ArrayList<Integer>();
sum = 0;

Thread divisibleBy_3 = new Thread(new Divide(n,3));
Thread divisibleBy_5 = new Thread(new Divide(n,5));
Thread divisibleBy_7 = new Thread(new Divide(n,7));
divisibleBy_3.start();
divisibleBy_5.start();
divisibleBy_7.start();

// TODO
try {
divisibleBy_3.join();
divisibleBy_5.join();
divisibleBy_7.join();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}

return sum;
}
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/sbu/cs/UseInterrupts.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {

System.out.println(Thread.currentThread().getName() + " has been interrupted");
break;
}
finally {
this.sleepCounter--;
Expand Down Expand Up @@ -66,11 +67,13 @@ public LoopThread(int value) {
public void run() {
System.out.println(this.getName() + " is Active.");

for (int i = 0; i < 10; i += 3)
for (int i = 0; i < 10 && !Thread.currentThread().isInterrupted(); i += 3)
{
i -= this.value;

}
if (Thread.currentThread().isInterrupted())
System.out.println(Thread.currentThread().getName() + " has been interrupted");
}
}

Expand All @@ -83,11 +86,27 @@ public static void main(String[] args) {
sleepThread.start();

// TODO Check if this thread runs for longer than 3 seconds (if it does, interrupt it)
try {
sleepThread.join(3000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
if(sleepThread.isAlive()) {
sleepThread.interrupt();
}

LoopThread loopThread = new LoopThread(3);
loopThread.start();

// TODO Check if this thread runs for longer than 3 seconds (if it does, interrupt it)
try {
loopThread.join(3000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
if(loopThread.isAlive()) {
loopThread.interrupt();
}

}
}
}