From 5cc73936689ac602810dcfe885084ef36c7cae82 Mon Sep 17 00:00:00 2001 From: Kiana Ghamsari Date: Thu, 4 May 2023 00:24:05 +0430 Subject: [PATCH] Done --- .gradle/7.4.2/checksums/checksums.lock | Bin 0 -> 17 bytes .../dependencies-accessors.lock | Bin 0 -> 17 bytes .../dependencies-accessors/gc.properties | 0 .../executionHistory/executionHistory.lock | Bin 0 -> 17 bytes .gradle/7.4.2/fileChanges/last-build.bin | Bin 0 -> 1 bytes .gradle/7.4.2/fileHashes/fileHashes.lock | Bin 0 -> 17 bytes .gradle/7.4.2/gc.properties | 0 .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .gradle/buildOutputCleanup/cache.properties | 4 +- .gradle/buildOutputCleanup/outputFiles.bin | Bin 19325 -> 0 bytes Report.md | 25 ++++++++ src/main/java/sbu/cs/CPU_Simulator.java | 58 ++++++++++++++++-- src/main/java/sbu/cs/FindMultiples.java | 49 ++++++++++++++- src/main/java/sbu/cs/UseInterrupts.java | 25 +++++++- 14 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 .gradle/7.4.2/checksums/checksums.lock create mode 100644 .gradle/7.4.2/dependencies-accessors/dependencies-accessors.lock create mode 100644 .gradle/7.4.2/dependencies-accessors/gc.properties create mode 100644 .gradle/7.4.2/executionHistory/executionHistory.lock create mode 100644 .gradle/7.4.2/fileChanges/last-build.bin create mode 100644 .gradle/7.4.2/fileHashes/fileHashes.lock create mode 100644 .gradle/7.4.2/gc.properties delete mode 100644 .gradle/buildOutputCleanup/outputFiles.bin create mode 100644 Report.md diff --git a/.gradle/7.4.2/checksums/checksums.lock b/.gradle/7.4.2/checksums/checksums.lock new file mode 100644 index 0000000000000000000000000000000000000000..7e853d1cc929a48f0d1ee072e03d507ea14062a8 GIT binary patch literal 17 TcmZP;D%`&;^Q!Q61}FdkFe3x> literal 0 HcmV?d00001 diff --git a/.gradle/7.4.2/dependencies-accessors/dependencies-accessors.lock b/.gradle/7.4.2/dependencies-accessors/dependencies-accessors.lock new file mode 100644 index 0000000000000000000000000000000000000000..971a911fffc61b926eb53357154064f3eb24afe1 GIT binary patch literal 17 ScmZP;spDN2(srkU0SW*p^aFnY literal 0 HcmV?d00001 diff --git a/.gradle/7.4.2/dependencies-accessors/gc.properties b/.gradle/7.4.2/dependencies-accessors/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/7.4.2/executionHistory/executionHistory.lock b/.gradle/7.4.2/executionHistory/executionHistory.lock new file mode 100644 index 0000000000000000000000000000000000000000..3ac171678f91e9e69d5f961f83990248cf0beeb5 GIT binary patch literal 17 TcmZS1KlbY*|1#-I3{U_7JpBa5 literal 0 HcmV?d00001 diff --git a/.gradle/7.4.2/fileChanges/last-build.bin b/.gradle/7.4.2/fileChanges/last-build.bin new file mode 100644 index 0000000000000000000000000000000000000000..f76dd238ade08917e6712764a16a22005a50573d GIT binary patch literal 1 IcmZPo000310RR91 literal 0 HcmV?d00001 diff --git a/.gradle/7.4.2/fileHashes/fileHashes.lock b/.gradle/7.4.2/fileHashes/fileHashes.lock new file mode 100644 index 0000000000000000000000000000000000000000..cc1018cb381689e9de1902df1d71525e9e9bdee2 GIT binary patch literal 17 TcmZRcwCRLE-^b-|7@zumAu6 diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties index 1608cbe..db44efb 100644 --- a/.gradle/buildOutputCleanup/cache.properties +++ b/.gradle/buildOutputCleanup/cache.properties @@ -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 diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin deleted file mode 100644 index 813a0936e87362d6c73158731fc885e41206d560..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 19325 zcmeI%YfQ~?9LMpKE^>6g=*XsfrlLZo5n9E+&TwLsE-Z^w?x%}Uo7`h7Eu>nkjkQ!V zjaZXu6y>tARMY4p)9ryf<;cnJ_dRCk$ytjB_Bq=*{rCER|KHQ^y`JSS5=l%(_)GNr zW%>IOS15o2D1ZVefC4Ch0w{n2D1ZVefC4Ch0w{n2D1ZVefCB$Zfwjtmh}mHzZWBf? zjFUwo4+G`J>;p$-cFX#{Zeq6sqv-z!hc_;%*(*ue&pn4ceU{02pU!zpmjXZtJ$O`3K)qNq(a# zwJCSSfP%Y}ymIJRZCLBILB6Mvyv}nV^ZU|oHWut#Rqm<9TQ2FO1MN&Y;5T9R8 zK6vE9;)*qvM%?4c6|H%?amAf^%mZJMi=@+a>ziBd^8GW&wGUQb%xx|>!{?8X>tAkK zXPj?-i@Oi`q+y#4G2)g!=0Z2Q_VwDpnrJcaFZhzX_xn99a_$Ia^MMn|y$5_k+P$n&xKAYya2j@vn64+|zKUFy zR&aHWwtWM0A(=c>E3?bO>eN;4pU5M;gw{guXEl6JF?r;oLWPl2RW@_!dh#t@??{AocOtf zJ9HyalJR8Eq0fzcK3nCoNN2^qll<=_XplVB?(DRMwI?I_d>T3Z6p3`A*gbrr01BW0 z3ZMWApa2S>01BW03ZMWApa2S>01BW03ZMWApa2S>01BW03ZMWApa2S>01BW03ZMWA zpa2S>01Et#0&|s1Cn?`rqwmoVqd!Cm|4hdA?dnfyBI9)~O}YK;5NBya> XZ62?u)c0s2nw&y?k0wv4&-{{~ startSimulation(ArrayList tasks) { ArrayList executedTasks = new ArrayList<>(); - // TODO + ArrayList 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 sort(ArrayList tasks){ + for(int i=0;itasks.get(j).processingTime){ + Collections.swap(tasks,i,j); + } + } + } + return tasks; + } + public static void main(String[] args) { } } diff --git a/src/main/java/sbu/cs/FindMultiples.java b/src/main/java/sbu/cs/FindMultiples.java index 5467003..1f5fd75 100644 --- a/src/main/java/sbu/cs/FindMultiples.java +++ b/src/main/java/sbu/cs/FindMultiples.java @@ -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 @@ -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 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(); + 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; } diff --git a/src/main/java/sbu/cs/UseInterrupts.java b/src/main/java/sbu/cs/UseInterrupts.java index a7b7d49..b867119 100644 --- a/src/main/java/sbu/cs/UseInterrupts.java +++ b/src/main/java/sbu/cs/UseInterrupts.java @@ -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--; @@ -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"); } } @@ -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(); + } } -} +} \ No newline at end of file