Difference between revisions of "Join All Assignment"
(Created page with "=Group Warmup= =Background= [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html ConcurrentLinkedQueue] is a thread safe data structure....") |
|||
Line 1: | Line 1: | ||
=Group Warmup= | =Group Warmup= | ||
=Background= | =Background= | ||
− | [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html ConcurrentLinkedQueue] is a thread safe data structure. Multiple threads can add and remove from it without fear. The order they will be in might not be deterministic, but it won't lose anybody. We will end up using a ConcurrentLinkedQueue to track all of the spawned Futures in the [[# | + | [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html ConcurrentLinkedQueue] is a thread safe data structure. Multiple threads can add and remove from it without fear. The order they will be in might not be deterministic, but it won't lose anybody. We will end up using a ConcurrentLinkedQueue to track all of the spawned Futures in the [[#Quicksort_Assignment|Quicksort]] exercise section of this lab. |
Sadly, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html ConcurrentLinkedQueue's documentation] reports that: | Sadly, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html ConcurrentLinkedQueue's documentation] reports that: |
Latest revision as of 15:38, 3 November 2022
Group Warmup
Background
ConcurrentLinkedQueue is a thread safe data structure. Multiple threads can add and remove from it without fear. The order they will be in might not be deterministic, but it won't lose anybody. We will end up using a ConcurrentLinkedQueue to track all of the spawned Futures in the Quicksort exercise section of this lab.
Sadly, ConcurrentLinkedQueue's documentation reports that:
- "Iterators are weakly consistent, returning elements reflecting the state of the queue at some point at or since the creation of the iterator."
This means that if all of the items aren't in the queue when you start iterating through them, then you are not guaranteed to get updated when new items get added.
Therefore, using the standard iterating for loop from ThreadsRightNow
will NOT work for ThreadsEventually
:
/* package-private */ static int joinAllInQueueViaIteration(Queue<Thread> queue) throws InterruptedException { int count = 0; for (Thread thread : queue) { thread.join(); count++; } return count; }
Think about how you can make sure that all of threads are joined.
Code To Implement
class: | ThreadsEventually.java | |
methods: | joinAllInQueueViaPoll | |
package: | joinall.group | |
source folder: | student/src/main/java |
method: private static int joinAllInQueueViaPoll(Queue<Thread> queue)
(sequential implementation only)
NOTE: this method should return the number of threads joined.
Completing this optional warm up will help you when you implement Parallel Quicksort.
Testing
class: | JoinAllTestSuite.java | |
package: | joinall.group | |
source folder: | testing/src/test/java |