diff --git a/.gradle/7.4.2/checksums/checksums.lock b/.gradle/7.4.2/checksums/checksums.lock new file mode 100644 index 0000000..7e853d1 Binary files /dev/null and b/.gradle/7.4.2/checksums/checksums.lock differ 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 0000000..971a911 Binary files /dev/null and b/.gradle/7.4.2/dependencies-accessors/dependencies-accessors.lock differ 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 0000000..3ac1716 Binary files /dev/null and b/.gradle/7.4.2/executionHistory/executionHistory.lock differ 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 0000000..f76dd23 Binary files /dev/null and b/.gradle/7.4.2/fileChanges/last-build.bin differ diff --git a/.gradle/7.4.2/fileHashes/fileHashes.lock b/.gradle/7.4.2/fileHashes/fileHashes.lock new file mode 100644 index 0000000..cc1018c Binary files /dev/null and b/.gradle/7.4.2/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.4.2/gc.properties b/.gradle/7.4.2/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 42a5cb6..6c5893d 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ 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 813a093..0000000 Binary files a/.gradle/buildOutputCleanup/outputFiles.bin and /dev/null differ diff --git a/Report.md b/Report.md new file mode 100644 index 0000000..ddee557 --- /dev/null +++ b/Report.md @@ -0,0 +1,25 @@ +# 6th Assignment Report + +![](https://github.com/kianaghamsari/Second-Assignment/blob/develop/uni.png) + +## Kiana Ghamsari - 400222079 + + +# Introduction + +The purpose of this project is to review the concepts of multithreaded programming and utilize them correctly. + + +# Design and Implementation + +The following exercises have been completed: +* `CPU Simulator`: The `sort()` function has been implemented to sort tasks based on their time and then a for loop is created to create, run, and wait for them to complete. +* `Find Multiples`: An array list of unique integers has been created to store multiples of 3, 5, and 7, each handled by 3 separate tasks. Then, the sum of them will be returned. The `synchronized` keyword is used to handle race conditions. +* `Use Interrupts`: The interruptions have been handled in each task, and, each thread is terminated after 3000 milliseconds using the `interrupt()` method. + + +# Conclusion + +Threads allow a program to operate more efficiently by doing multiple things at the same time. +Multithreading allows concurrent execution of two or more parts of a program. In this case, handling race conditions is important. They occur when two threads operate on the same object without proper synchronization and their operation interleaves on each other. +An easy way to handle them is to use the `synchronized` keyword. diff --git a/src/main/java/sbu/cs/CPU_Simulator.java b/src/main/java/sbu/cs/CPU_Simulator.java index 6be88c3..1900ca5 100644 --- a/src/main/java/sbu/cs/CPU_Simulator.java +++ b/src/main/java/sbu/cs/CPU_Simulator.java @@ -1,8 +1,9 @@ package sbu.cs; import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +// import java.util.Arrays; +import java.util.Collections; +// import java.util.List; /* For this exercise, you must simulate a CPU with a single core. @@ -22,8 +23,26 @@ 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; + } + + public long getProcessingTime() { + return processingTime; + } + + public void setProcessingTime(long processingTime) { + this.processingTime = processingTime; + } + + public String getID() { + return ID; + } + + public void setID(String iD) { + ID = iD; } /* @@ -32,7 +51,12 @@ public Task(String ID, long processingTime) { */ @Override public void run() { - // TODO + try { + Thread.sleep(processingTime); + } catch (InterruptedException e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } } } @@ -44,11 +68,35 @@ Here the CPU selects the next shortest task to run (also known as the public ArrayList 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