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 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.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Report to assignment number 6.pdf
Binary file not shown.
49 changes: 40 additions & 9 deletions src/main/java/sbu/cs/CPU_Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/*
For this exercise, you must simulate a CPU with a single core.
Expand All @@ -17,22 +18,26 @@
Use the tests provided in the test folder to ensure your code works correctly.
*/

public class CPU_Simulator
{
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;
}

/*
Simulate running a task by utilizing the sleep method for the duration of
the task's processingTime. The processing time is given in milliseconds.
*/
/*
Simulate running a task by utilizing the sleep method for the duration of
the task's processingTime. The processing time is given in milliseconds.
*/
@Override
public void run() {
// TODO
try {
Thread.sleep(processingTime);
} catch (InterruptedException e) {
}
}
}

Expand All @@ -44,11 +49,37 @@ 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
while (tasks.size()!=0) {
Task shortest = tasks.get(0);
for (Task i : tasks) {
if (i.processingTime < shortest.processingTime) {
shortest = i;
}
}
tasks.remove(shortest);
Thread thread = new Thread(shortest);
thread.start();
try {
thread.join();
executedTasks.add(shortest.ID);
} catch (InterruptedException e) {
}
}

return executedTasks;
}

public static void main(String[] args) {

ArrayList<Task> tasks = new ArrayList<>();
CPU_Simulator cpu = new CPU_Simulator();
tasks.add(new Task("1", 200));
tasks.add(new Task("2", 1236));
tasks.add(new Task("3", 2));

ArrayList<String> executedTasks = cpu.startSimulation(tasks);
for (String i : executedTasks) {
System.out.println(i);
}
}
}
55 changes: 52 additions & 3 deletions src/main/java/sbu/cs/FindMultiples.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,36 @@

Use the tests provided in the test folder to ensure your code works correctly.
*/

import java.util.*;
public class FindMultiples
{

public static ArrayList<Integer> dividends = new ArrayList<>();
//public static int temp = 0;
public static synchronized void increment(int temp){
dividends.add(temp);
}
public static class thread implements Runnable{

public int divisor;
public int n;

public thread(int n , int divisor){
this.n=n;
this.divisor=divisor;
}

@Override
public void run() {

for (int i=1 ; i<=n ; i++){
if (i%divisor==0){
//temp=i;
increment(i);
}
}
}
}
// TODO create the required multithreading class/classes using your preferred method.


Expand All @@ -31,12 +57,35 @@ public class FindMultiples
*/
public int getSum(int n) {
int sum = 0;

Thread t1 = new Thread(new thread(n,3));
Thread t2 = new Thread(new thread(n,7));
Thread t3 = new Thread(new thread(n,5));
// TODO
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
}catch (InterruptedException e){

}
Set<Integer> set = new HashSet<>(dividends);
dividends.clear();
dividends.addAll(set);

for (int i : dividends){
sum+=i;
}

return sum;
}

public static void main(String[] args) {

FindMultiples obj = new FindMultiples();
Scanner input = new Scanner(System.in);
System.out.println(obj.getSum(input.nextInt()));
}
}
}
40 changes: 20 additions & 20 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 Down Expand Up @@ -47,14 +47,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 @@ -74,10 +74,10 @@ public void run() {
}
}

/*
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.
*/
/*
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) {
SleepThread sleepThread = new SleepThread(5);
sleepThread.start();
Expand All @@ -90,4 +90,4 @@ public static void main(String[] args) {
// TODO Check if this thread runs for longer than 3 seconds (if it does, interrupt it)

}
}
}