diff --git a/.gradle/7.5.1/executionHistory/executionHistory.bin b/.gradle/7.5.1/executionHistory/executionHistory.bin index 69d19e6..ee4faa8 100644 Binary files a/.gradle/7.5.1/executionHistory/executionHistory.bin and b/.gradle/7.5.1/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.5.1/executionHistory/executionHistory.lock b/.gradle/7.5.1/executionHistory/executionHistory.lock index 74fe4a6..7a3c316 100644 Binary files a/.gradle/7.5.1/executionHistory/executionHistory.lock and b/.gradle/7.5.1/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.5.1/fileHashes/fileHashes.bin b/.gradle/7.5.1/fileHashes/fileHashes.bin index b8a9c92..54c2a94 100644 Binary files a/.gradle/7.5.1/fileHashes/fileHashes.bin and b/.gradle/7.5.1/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.5.1/fileHashes/fileHashes.lock b/.gradle/7.5.1/fileHashes/fileHashes.lock index be289e7..45d8cab 100644 Binary files a/.gradle/7.5.1/fileHashes/fileHashes.lock and b/.gradle/7.5.1/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.5.1/fileHashes/resourceHashesCache.bin b/.gradle/7.5.1/fileHashes/resourceHashesCache.bin index ae66883..7f968d3 100644 Binary files a/.gradle/7.5.1/fileHashes/resourceHashesCache.bin and b/.gradle/7.5.1/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 42a5cb6..7f88905 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin index 813a093..b9c847b 100644 Binary files a/.gradle/buildOutputCleanup/outputFiles.bin and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe index d6b6958..b3a0d78 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Report to assignment number 6.pdf b/Report to assignment number 6.pdf new file mode 100644 index 0000000..5353e29 Binary files /dev/null and b/Report to assignment number 6.pdf differ diff --git a/src/main/java/sbu/cs/CPU_Simulator.java b/src/main/java/sbu/cs/CPU_Simulator.java index 6be88c3..1c4eee8 100644 --- a/src/main/java/sbu/cs/CPU_Simulator.java +++ b/src/main/java/sbu/cs/CPU_Simulator.java @@ -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. @@ -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) { + } } } @@ -44,11 +49,37 @@ Here the CPU selects the next shortest task to run (also known as the public ArrayList startSimulation(ArrayList tasks) { ArrayList 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 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 executedTasks = cpu.startSimulation(tasks); + for (String i : executedTasks) { + System.out.println(i); + } } } diff --git a/src/main/java/sbu/cs/FindMultiples.java b/src/main/java/sbu/cs/FindMultiples.java index 5467003..c51523f 100644 --- a/src/main/java/sbu/cs/FindMultiples.java +++ b/src/main/java/sbu/cs/FindMultiples.java @@ -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 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. @@ -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 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())); } -} +} \ No newline at end of file diff --git a/src/main/java/sbu/cs/UseInterrupts.java b/src/main/java/sbu/cs/UseInterrupts.java index a7b7d49..0f192be 100644 --- a/src/main/java/sbu/cs/UseInterrupts.java +++ b/src/main/java/sbu/cs/UseInterrupts.java @@ -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; @@ -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) { @@ -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(); @@ -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) } -} +} \ No newline at end of file