diff --git a/.gradle/7.5.1/executionHistory/executionHistory.bin b/.gradle/7.5.1/executionHistory/executionHistory.bin index 69d19e6..8d95d44 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..359aaad 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..a375eb5 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..cf4c9c8 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..975f87a 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..4c85cdd 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..f984c87 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..d786277 100644 Binary files a/.gradle/file-system.probe and b/.gradle/file-system.probe differ diff --git a/report.pdf b/report.pdf new file mode 100644 index 0000000..307834f Binary files /dev/null and b/report.pdf differ diff --git a/src/main/java/sbu/cs/CPU_Simulator.java b/src/main/java/sbu/cs/CPU_Simulator.java index 6be88c3..ab42718 100644 --- a/src/main/java/sbu/cs/CPU_Simulator.java +++ b/src/main/java/sbu/cs/CPU_Simulator.java @@ -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; } /* @@ -32,7 +33,11 @@ public Task(String ID, long processingTime) { */ @Override public void run() { - // TODO + try { + Thread.sleep(processingTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } @@ -44,7 +49,25 @@ Here the CPU selects the next shortest task to run (also known as the public ArrayList startSimulation(ArrayList tasks) { ArrayList 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; } diff --git a/src/main/java/sbu/cs/FindMultiples.java b/src/main/java/sbu/cs/FindMultiples.java index 5467003..040a10f 100644 --- a/src/main/java/sbu/cs/FindMultiples.java +++ b/src/main/java/sbu/cs/FindMultiples.java @@ -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 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 removeDuplicates(ArrayList list) + { + + ArrayList 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 allMuls = new ArrayList(); + allMuls.addAll(mul3.muls); + allMuls.addAll(mul5.muls); + allMuls.addAll(mul7.muls); + + allMuls = removeDuplicates(allMuls); - // TODO + for (int i=0;i