Join All Assignment
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 |