Join All Assignment

From CSE231 Wiki
Jump to navigation Jump to search

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.

Drain the queue by repeatedly polling until it is empty. Be sure to actually join the threads.

Code To Implement

class: ThreadsEventually.java Java.png
methods: joinAllInQueueViaPoll
package: joinall.group
source folder: student/src/main/java

method: private static int joinAllInQueueViaPoll(Queue<Thread> queue) Sequential.svg (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 Junit.png
package: joinall.group
source folder: testing/src/test/java