From b8a4fe897dd50f7a58dbc872731d45472effe81d Mon Sep 17 00:00:00 2001 From: FarimaKafi <126360004+FarimaKafi@users.noreply.github.com> Date: Wed, 3 May 2023 22:05:29 +0430 Subject: [PATCH 1/3] complete cpu simulator class --- .../executionHistory/executionHistory.bin | Bin 40151 -> 40151 bytes .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/7.5.1/fileHashes/fileHashes.bin | Bin 20347 -> 20847 bytes .gradle/7.5.1/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../7.5.1/fileHashes/resourceHashesCache.bin | Bin 19143 -> 19245 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .gradle/buildOutputCleanup/outputFiles.bin | Bin 19325 -> 19649 bytes .gradle/file-system.probe | Bin 8 -> 8 bytes src/main/java/sbu/cs/CPU_Simulator.java | 43 ++++++++++++++++-- 9 files changed, 40 insertions(+), 3 deletions(-) diff --git a/.gradle/7.5.1/executionHistory/executionHistory.bin b/.gradle/7.5.1/executionHistory/executionHistory.bin index 69d19e678295c51070fc0de456fde229675ecc60..56e8ea12b3eb90d5cd2da0d90a184a0d8ecfc3e7 100644 GIT binary patch delta 690 zcmcbfUNSmc#e^2878S?1B^G7oCdPQCq$UOw^R9B}bn@~!>;8Rx{Mqy=OV>{QXRwWJvVN|(M2yDLz3M$bm3nWbPnkRK zQtlkEQspOSh8W6y sC9HHpsJ@)MXLy_BO4a+;Gp96VUpi)V1MHnIOe~BH3|!iqGYax`06s4qr2qf` delta 447 zcmcbP61_-d3Y~Z_PvuwIL zGvlktj3LgG<+4R5_a^a9&I@E0_~BXiLHuek*OA4_uiDEN?wEWaOB$|BQ^&id)Jp#P z&fiu>xy4O?qvkWtDo!fZPcGJX%gjshEzK#(EXYYM*2_vPOGKD`DqBS0wMOzf$MU+U zpXcTVm-VyqOn#RwGx=(g$K-%OPCwGm%`g$8D2ln`6m9t)XhJB8?0nuoHDtvSXhcdp#QMD^@+76S=)}=9xA$H z%W(;;;0XKV$Xs>SDV!Z diff --git a/.gradle/7.5.1/fileHashes/fileHashes.bin b/.gradle/7.5.1/fileHashes/fileHashes.bin index b8a9c92457490aa40570b309b1d9be60e390413e..d330d9d42334fd3a7cb95c7be4d9125944f2ec12 100644 GIT binary patch delta 657 zcmex8kMaE?#tkMCn))sA_J=AYYN{E)z;ph8C}>X>IaK!9P6#6IUp?7X!a}3>XZX4% zUb`a@!GN1E#ZzK#>jup*7K4Zfa!)Rmun;)?^4tz!6*Jbfr_p3~HwB=2S@sMgixK)6Qg=@uWgT z{nt*OE2YTKyy3-ct5PMXcIU}kr7Q$uYUVl4xA`6mRd|2$Q>jZ#ER`D-e~521SRlOF z(Zh!Ip6xVZY4 zbDrwdhFONwYJRDhgLTZ}1t|gomYqNXO~>9H#(^tacD$L(aNAt6Uch`2sur%pm|BkQ z^R9B}bn@~!>;8Rx{MmGfvx268tYBbZ1_cnBB{3RH_p0~&RO-EzK4tE_OSw=TbHExH zIA6hZOn&bx$MR@((7efleo_*)3;#^@jtNehZxdqU>Cff?)wO0ax4#}p)vC$1{!(Fj zj;^(u%VautvYq0Vd9EN2RkapkHq#53*@$43_Y7~7T&a5Bdghd->`TXtpgL|qbTF2H GbN~SF$-t!m delta 80 zcmaFAi1GJ4#tkMCf~qa?_J=AYYN{E)z;piOSP6^CM iXBAXpOtk?lo${S4p diff --git a/.gradle/7.5.1/fileHashes/fileHashes.lock b/.gradle/7.5.1/fileHashes/fileHashes.lock index be289e785a2dc13329ec7b044e06aa54e5912c03..c31afc149e3e08cc7ba09b068966cf4fe4ef05d4 100644 GIT binary patch literal 17 UcmZRceqf>e*T2hS7$9H`06-oEyZ`_I literal 17 UcmZRceqf>e*T2hS7$9Ia06+}|s{jB1 diff --git a/.gradle/7.5.1/fileHashes/resourceHashesCache.bin b/.gradle/7.5.1/fileHashes/resourceHashesCache.bin index ae668839f972f4e829b3c70c93d5e82e8485fcc4..c9e75a8b44046239cdd926e0dade39837896eef8 100644 GIT binary patch delta 220 zcmX>;m2vGf#tkMCnqrMkit9^?cJeTQf!EUiP|%drl48OXy#XqIe6p*Ag~qys`Lk}_ zJU$H~=*V!Z delta 52 zcmZ2Gjq&(Y#tkMCg2IhXit9^?cJeTQf!ET>u@V-Wk4khgGBMuVsQ5#Cqk)6iW=D@U FCIG_x5dr`J diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 42a5cb69545d7d4deaf02419667d56ca26b13d31..31b15fc5cd58017cd0633154e0921b36ec7d47ab 100644 GIT binary patch literal 17 UcmZS9neslVwJ7f(0|aye05p6A!~g&Q literal 17 UcmZS9neslVwJ7f(0|b-;05oU>umAu6 diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin index 813a0936e87362d6c73158731fc885e41206d560..d47de0995dd42ef86474d4c52b07407422001d52 100644 GIT binary patch delta 433 zcmex6jq%`2#tkMCj1H4cB`O5EZWXQla=E0C0SvrrCoh#~5Lj5XG2|$thC5WSf3mD( zhQQ%n&dn7ZcNRkgeVixPN?Hh<`gE+8*>&49sNmemYb6x~E=%rKVEvZp0@c27@=>75 z`;V7je%IT>3l%&%`Kx4r!#m^7wfRqX&WCE}{SO7dI#|_w89q;gil@WG|7LzINS)%m z5h}iZvaM7ge{7Z0l};5is95*pRw+yVpn^)(hLsZ_V%{$&Z~h)N2UWRwvMW%~Tcc|q>sgi~P?d(0W2GAe zvaUE~oj%CC8>;g93CS delta 95 zcmX>&lkx8~#tkMCjCzwzB`PK#1>#W2gvn?C4>`3; startSimulation(ArrayList tasks) { ArrayList executedTasks = new ArrayList<>(); - // TODO + while (!tasks.isEmpty()) { + // Find the task with the shortest processing time + Task shortestTask = tasks.get(0); + for (Task task : tasks) { + if (task.processingTime < shortestTask.processingTime) { + shortestTask = task; + } + } + // Remove the shortest task from the list and create a new thread for it + tasks.remove(shortestTask); + Thread thread = new Thread(shortestTask); + + // Start the thread, wait for it to finish, and add its ID to the executed tasks list + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + executedTasks.add(shortestTask.ID); + } return executedTasks; } public static void main(String[] args) { + + // Create some tasks for testing + ArrayList tasks = new ArrayList<>(); + tasks.add(new Task("Task 1", 3000)); + tasks.add(new Task("Task 2", 1000)); + tasks.add(new Task("Task 3", 2000)); + + // Start the simulation and print the list of executed tasks + CPU_Simulator simulator = new CPU_Simulator(); + ArrayList executedTasks = simulator.startSimulation(tasks); + System.out.println("Executed tasks: " + executedTasks); + } } From f3b37b5239a3f05ae49d8cfc7d4ed49d69586858 Mon Sep 17 00:00:00 2001 From: FarimaKafi <126360004+FarimaKafi@users.noreply.github.com> Date: Wed, 3 May 2023 22:24:43 +0430 Subject: [PATCH 2/3] complete multiples class --- .../executionHistory/executionHistory.bin | Bin 40151 -> 40151 bytes .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/7.5.1/fileHashes/fileHashes.bin | Bin 20847 -> 20897 bytes .gradle/7.5.1/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../7.5.1/fileHashes/resourceHashesCache.bin | Bin 19245 -> 19347 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes src/main/java/sbu/cs/FindMultiples.java | 62 +++++++++++++++++- 7 files changed, 59 insertions(+), 3 deletions(-) diff --git a/.gradle/7.5.1/executionHistory/executionHistory.bin b/.gradle/7.5.1/executionHistory/executionHistory.bin index 56e8ea12b3eb90d5cd2da0d90a184a0d8ecfc3e7..e56591703817026d68f9d7be0fe406daf73c921c 100644 GIT binary patch delta 402 zcmcbV73rh-0G7Bor3K<|EV6uVl zmdlbHp?Ro0kRzp%IwmtcFV^=rP+LXXz5Kf>*+fi`+8vQhZBufG*2PEmncELK2I!^^$WE zi;EcqG=FUSf4=nK`zAldrd})26;o&b2bw;EgK-LqW|;CjzK$K}xi4=Iqa5G;!-u|2``(x~T`xo7bqV7 delta 345 zcmcb+rwRdo%ZUH!pg;IXs(% zQDAB68N-^Jd$cxWw)6-aeW{C47Mocj8 znY;H2KW9_nyqXw@I#4y>C5p kN>lcwV@5Z?hJRsVVFZSo_9A95ZM=D3w;1#0w&Fld00Oy^Z2$lO diff --git a/.gradle/7.5.1/executionHistory/executionHistory.lock b/.gradle/7.5.1/executionHistory/executionHistory.lock index 19af956fe82c684a45f02fc847fd8a2ea41c02bf..9e5c2a24bfbad961a1b85138146f6fcbe5fcddf7 100644 GIT binary patch literal 17 UcmZSHvO;ss+Bc$`86dz706F;t+yDRo literal 17 UcmZSHvO;ss+Bc$`86dzK06Fdi(EtDd diff --git a/.gradle/7.5.1/fileHashes/fileHashes.bin b/.gradle/7.5.1/fileHashes/fileHashes.bin index d330d9d42334fd3a7cb95c7be4d9125944f2ec12..60b401010234c5ba940cc58e689a1a4aaa37e567 100644 GIT binary patch delta 365 zcmaFAh;iX!#tkMCj0T%cB_x;yY?g1I6rUrb$p8j{`I}v(lNlK&ZdCjszR{pVc(bF2 z2Yb-#`$-AYUEM_?AgyN%BtWJpg0O(s%j!qIZSUg> zm<$&>>8)3xR2Pg>yJgX<)_IIgI7J9zYWiS6ZJ6e2{@C{aeCfgWO@4|^y;h{2{*4V1e*vM-LD7Aj5esJfRG~S-3ao z31=VV}DY=5-c_YVUY@O%YQdd5IP6NnW-Sm5B|>R--zs#6X-x3!TDI%WjbaRZ`* Iu>@oX06|4h+5i9m diff --git a/.gradle/7.5.1/fileHashes/fileHashes.lock b/.gradle/7.5.1/fileHashes/fileHashes.lock index c31afc149e3e08cc7ba09b068966cf4fe4ef05d4..bdf9b213a341593abfd9a50d349d1cdcf59362b5 100644 GIT binary patch literal 17 UcmZRceqf>e*T2hS7$D#f06;Va(f|Me literal 17 UcmZRceqf>e*T2hS7$9H`06-oEyZ`_I diff --git a/.gradle/7.5.1/fileHashes/resourceHashesCache.bin b/.gradle/7.5.1/fileHashes/resourceHashesCache.bin index c9e75a8b44046239cdd926e0dade39837896eef8..ab87a016264195e1579d26edd0a0c586aa55677d 100644 GIT binary patch delta 216 zcmZ2GjdAjH#tkMCnv#u9it9^?cJeTQf!EUiP|z5axk@NO?*>HNTX(aogaf0%f5T@2 zKcc=Lgo^r4u9XZBu>7)($?As6EvR7O3IJQIK~(?% delta 55 zcmbO{opJ3n#tkMCf?|zMit9^?cJeTQf!ET_u@Vl9lebEGOcs@DW>nm$_(Oc7!4J{R IjvjBA0Oht4G5`Po diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 31b15fc5cd58017cd0633154e0921b36ec7d47ab..a54cb418d87aefc6b496a3a9dd9094de12b7dd22 100644 GIT binary patch literal 17 UcmZS9neslVwJ7f(0|ZP305pgM&;S4c literal 17 UcmZS9neslVwJ7f(0|aye05p6A!~g&Q diff --git a/src/main/java/sbu/cs/FindMultiples.java b/src/main/java/sbu/cs/FindMultiples.java index 5467003..0806161 100644 --- a/src/main/java/sbu/cs/FindMultiples.java +++ b/src/main/java/sbu/cs/FindMultiples.java @@ -1,5 +1,11 @@ package sbu.cs; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + /* 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 @@ -22,7 +28,7 @@ public class FindMultiples { - // TODO create the required multithreading class/classes using your preferred method. + private static final int THREAD_POOL_SIZE = 4; // Set the thread pool size as needed /* @@ -32,11 +38,61 @@ public class FindMultiples public int getSum(int n) { int sum = 0; - // TODO + Set results = new HashSet<>(); // store unique multiples + + // Create a thread pool to execute tasks in parallel + ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE); + + // Divide the range [1, n] into smaller chunks to be processed by different threads + int chunkSize = (n + THREAD_POOL_SIZE - 1) / THREAD_POOL_SIZE; + for (int i = 0; i < THREAD_POOL_SIZE; i++) { + int start = i * chunkSize + 1; + int end = Math.min(start + chunkSize - 1, n); + executor.execute(new MultiplesTask(start, end, results)); + } - return sum; + // Shutdown the thread pool + executor.shutdown(); + try { + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + for (int multiple : results) { + sum += multiple; + } + + return sum; + } + + private static class MultiplesTask implements Runnable { + private final int start; + private final int end; + private final Set results; + + public MultiplesTask(int start, int end, Set results) { + this.start = start; + this.end = end; + this.results = results; + } + + @Override + public void run() { + for (int i = start; i <= end; i++) { + if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0) { + results.add(i); + } + } + } } public static void main(String[] args) { + + FindMultiples findMultiples = new FindMultiples(); + int n = 10; + int sum = findMultiples.getSum(n); + System.out.println("Sum of multiples in the range [1, " + n + "] = " + sum); + } } From 8b430c56c883b7e23e996cfe879f5bd4674da096 Mon Sep 17 00:00:00 2001 From: FarimaKafi <126360004+FarimaKafi@users.noreply.github.com> Date: Wed, 3 May 2023 22:43:34 +0430 Subject: [PATCH 3/3] complete use interrupts class , push all classes --- .../executionHistory/executionHistory.bin | Bin 40151 -> 40151 bytes .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/7.5.1/fileHashes/fileHashes.bin | Bin 20897 -> 20897 bytes .gradle/7.5.1/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../7.5.1/fileHashes/resourceHashesCache.bin | Bin 19347 -> 19381 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes src/main/java/sbu/cs/UseInterrupts.java | 44 ++++++++++-------- 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/.gradle/7.5.1/executionHistory/executionHistory.bin b/.gradle/7.5.1/executionHistory/executionHistory.bin index e56591703817026d68f9d7be0fe406daf73c921c..641ddbada2cb7db9822f7f50efaa0400b3d862bf 100644 GIT binary patch delta 290 zcmcbTZW8H<4`yP|uW#M=n}QFHOV7O=C9eG!to$|yGlM|E%xV9&?*IJl zb+*u!Rk^t`Kq1q>C%?iYv)JnnFLFB7)o>IjG-2MG%>nn>Vj@|e9l0m@mN9Fxev0Pi k(|LOsMSg#YKdHFsil&Xht#2i(AN_B7x^dd(*#$N_0MBcOHvj+t delta 290 zcmcb#zh0d7;mnTCUl+vGwar{3tyY00-*7Tc2`x_b z%qvMPDk?1~Db`EQNh~gA5YTgUt<_v6)47xF6t~QC1^KmL1ADdU)ywdl!Wx=FL6L2ccZ4|=MHmB+Ti@*)nxW#jOQoor)X|I kowtWkgzx_0L*J%-Z%mu6m!a^wgmK>N|5G>5F0j!70Bikz^Z)<= diff --git a/.gradle/7.5.1/executionHistory/executionHistory.lock b/.gradle/7.5.1/executionHistory/executionHistory.lock index 9e5c2a24bfbad961a1b85138146f6fcbe5fcddf7..749f4d2440cac84acc12e7aba6532e97a580e8c8 100644 GIT binary patch literal 17 UcmZSHvO;ss+Bc$`86dzH06G8!;{X5v literal 17 UcmZSHvO;ss+Bc$`86dz706F;t+yDRo diff --git a/.gradle/7.5.1/fileHashes/fileHashes.bin b/.gradle/7.5.1/fileHashes/fileHashes.bin index 60b401010234c5ba940cc58e689a1a4aaa37e567..ea302da40abed0253760e30f9ab1bdf3dc86bf13 100644 GIT binary patch delta 226 zcmZ3um~r7^#tm=%6*^MSDqecdbxQmEtDkH+SMM<}Ft$HBlkLF(2HXOZc>`9|ZxL}R z?6c(eTjMcXP=$YSFGNvMHAE4MIgr*f1`?V;tO&va)e{Pjx9z#z`>%e6^mJF2VyKQP zpd=8mz5o(vI&uPeAKTa86nt=8dhX>Yac!uMTA)gOAZCfi)bjgF{7J=4S2S%5Zhb3R O{pdebOVi}!Ksx}KL`2yD delta 142 zcmZ3um~r7^#tm=%7545h4qVx?`8R*e?7t z)jK9QX}(Q}ji*1G2UyYSpf!`Z{q+16i5LeJ5)R&$w5=T5d$+%nG<MG8A5yFoGrbpPe^3InWLO;Knwu diff --git a/.gradle/7.5.1/fileHashes/fileHashes.lock b/.gradle/7.5.1/fileHashes/fileHashes.lock index bdf9b213a341593abfd9a50d349d1cdcf59362b5..4dee4dcb752be5611f8dabbe39e15e3018c53005 100644 GIT binary patch literal 17 UcmZRceqf>e*T2hS7$D#b06;zk+yDRo literal 17 UcmZRceqf>e*T2hS7$D#f06;Va(f|Me diff --git a/.gradle/7.5.1/fileHashes/resourceHashesCache.bin b/.gradle/7.5.1/fileHashes/resourceHashesCache.bin index ab87a016264195e1579d26edd0a0c586aa55677d..fd6acefd0f8565ea4fd489081988ba624e9cac93 100644 GIT binary patch delta 87 zcmbO{opI}Q#tkMCj8dCTC4Mjp_#4h!&i%>oH3JxUPoAtQRm|A9QSpcPMuQomn;kuZ jn3arya*{wS2*Lu9)*ruxOj{WLG1+6?g_8RoKtVYGrrR3y delta 35 rcmdlwopJJX#tkMCjFOv8C4MkYc9qIz%-pE>Lwuvb2GPxq9zo0i 0) + while (this.sleepCounter > 0 && !Thread.currentThread().isInterrupted()) { try { Thread.sleep(1000); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } finally { this.sleepCounter--; @@ -44,17 +39,14 @@ public void run() { } } + if (Thread.currentThread().isInterrupted()) { + System.out.println(this.getName() + " has been interrupted"); + } + } } -/* - 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) { @@ -66,11 +58,15 @@ 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(this.getName() + " has been interrupted"); + } } } @@ -82,12 +78,22 @@ public static void main(String[] args) { SleepThread sleepThread = new SleepThread(5); sleepThread.start(); - // TODO Check if this thread runs for longer than 3 seconds (if it does, interrupt it) + try { + Thread.sleep(3000); + sleepThread.interrupt(); + } catch (InterruptedException e) { + e.printStackTrace(); + } LoopThread loopThread = new LoopThread(3); loopThread.start(); - // TODO Check if this thread runs for longer than 3 seconds (if it does, interrupt it) + try { + Thread.sleep(3000); + loopThread.interrupt(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } }