Skip to content
Open

. #9

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 modified .gradle/7.5.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.5.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.5.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.5.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.5.1/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
Binary file added report.pdf
Binary file not shown.
29 changes: 26 additions & 3 deletions src/main/java/sbu/cs/CPU_Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static class Task implements Runnable {
long processingTime;
String ID;
public Task(String ID, long processingTime) {
// TODO
this.ID = ID;
this.processingTime = processingTime;
}

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

Expand All @@ -44,7 +49,25 @@ 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
Task min = tasks.get(0);
int n = tasks.size();
Task temp;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(tasks.get(j-1).processingTime > tasks.get(j).processingTime){
temp = tasks.get(j-1);
tasks.set(j-1,tasks.get(j));
tasks.set(j,temp);
}
}
}

for (Task task:tasks){
Thread thread = new Thread(task);
thread.start();
executedTasks.add(task.ID);
}


return executedTasks;
}
Expand Down
66 changes: 64 additions & 2 deletions src/main/java/sbu/cs/FindMultiples.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,82 @@
Use the tests provided in the test folder to ensure your code works correctly.
*/

import java.lang.reflect.Array;
import java.util.ArrayList;

public class FindMultiples
{

// TODO create the required multithreading class/classes using your preferred method.
public static class Mul implements Runnable{
public ArrayList<Integer> muls = new ArrayList<>();
public int first;
public int second;

public Mul(int first,int second){
this.first = first;
this.second = second;
}

@Override
public void run() {
for (int i=first;i<=second;i++){
if (i % first == 0){
muls.add(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) {

public static ArrayList<Integer> removeDuplicates(ArrayList<Integer> list)
{

ArrayList<Integer> newList = new ArrayList<>();
for (int element : list) {
if (!newList.contains(element)) {
newList.add(element);
}
}
return newList;
}

public static int getSum(int n) {
int sum = 0;
Mul mul3 = new Mul(3,n);
Thread t1 = new Thread(mul3);
t1.start();
Mul mul5 = new Mul(5,n);
Thread t2 = new Thread(mul5);
t2.start();
Mul mul7 = new Mul(7,n);
Thread t3 = new Thread(mul7);
t3.start();

try {
t1.join();
t2.join();
t3.join();
}
catch (InterruptedException e){
e.printStackTrace();
}


ArrayList<Integer> allMuls = new ArrayList<Integer>();
allMuls.addAll(mul3.muls);
allMuls.addAll(mul5.muls);
allMuls.addAll(mul7.muls);

allMuls = removeDuplicates(allMuls);

// TODO
for (int i=0;i<allMuls.size();i++){
sum+=allMuls.get(i);
}

return sum;
}
Expand Down
58 changes: 34 additions & 24 deletions src/main/java/sbu/cs/UseInterrupts.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

public class UseInterrupts
{
/*
TODO
Analyse the following class and add new code where necessary.
If an object from this type of thread is Interrupted, it must print this:
"{ThreadName} has been interrupted"
And then terminate itself.
*/
/*
TODO
Analyse the following class and add new code where necessary.
If an object from this type of thread is Interrupted, it must print this:
"{ThreadName} has been interrupted"
And then terminate itself.
*/
public static class SleepThread extends Thread {
int sleepCounter;

Expand All @@ -36,7 +36,8 @@ public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {

System.out.println(this.getName() + " has been interrupted.");
return;
}
finally {
this.sleepCounter--;
Expand All @@ -47,14 +48,14 @@ public void run() {
}
}

/*
TODO
Analyse the following class and add new code where necessary.
If an object from this type of thread is Interrupted, it must print this:
"{ThreadName} has been interrupted"
And then terminate itself.
(Hint: Use the isInterrupted() method)
*/
/*
TODO
Analyse the following class and add new code where necessary.
If an object from this type of thread is Interrupted, it must print this:
"{ThreadName} has been interrupted"
And then terminate itself.
(Hint: Use the isInterrupted() method)
*/
public static class LoopThread extends Thread {
int value;
public LoopThread(int value) {
Expand All @@ -69,25 +70,34 @@ public void run() {
for (int i = 0; i < 10; i += 3)
{
i -= this.value;

if (this.isInterrupted()) {
System.out.println(this.getName() + " has been interrupted.");
return;
}
}
}
}

/*
You can add new code to the main function. This is where you must utilize interrupts.
No existing line of code should be changed or deleted.
*/
public static void main(String[] args) {
/*
You can add new code to the main function. This is where you must utilize interrupts.
No existing line of code should be changed or deleted.
*/
public static void main(String[] args) throws InterruptedException {
SleepThread sleepThread = new SleepThread(5);
sleepThread.start();
Thread.sleep(3000);

// TODO Check if this thread runs for longer than 3 seconds (if it does, interrupt it)
if (sleepThread.isAlive()) {
sleepThread.interrupt();
}

LoopThread loopThread = new LoopThread(3);
loopThread.start();
Thread.sleep(3000);

// TODO Check if this thread runs for longer than 3 seconds (if it does, interrupt it)
if (loopThread.isAlive()) {
loopThread.interrupt();
}

}
}