The goal of this assignment is to gain experience working with concurrent programming via multiple threads and thread synchronization. Youll write an implementation that simulates producer and consumer processes. The simulated consummation process will take far more time to complete than the production process, causing the machine to run out of memory if the threads arent synchronized in such a way that no more than a specified number of units areproduced prior to being consumed.
1. [10] Create a new class with a public static void main method that first creates a java.util.concurrent.LinkedBlockingQueue with enough capacity for 100,000 String entries.
2. [20] Create a new class which implements Runnable to simulate the producer. When executed, this process should add 2,000,000 random Strings (hint: see the UUID class) to the above queue, waiting until there is space in the queue if necessary (i.e., using the LinkedBlockingQueue put() method). Print your progress once every 1000 Strings produced.
3. [40] Create another new class which implements Runnable to simulate the consumer. When executed, this process should consume Strings from the queue, keeping track of the overall max String found (i.e., using the String compareTo() method). The process should continue as long as there are Strings in the queue or the producer hasnt finished (i.e. so the consumer doesnt quit if the queue happens to be empty before the producer finishes). Use the Thread sleep()method to wait up to 10 milliseconds (i.e., a random number of milliseconds on the range [0, 10)) between each String comparison to ensure that the consumer takes longer to execute than the producer. Print your progress once every 1000 Strings consumed. Print the total number of Strings consumed and the max String found when the process completes.
There are some important subtleties to note here. Youll want to implement your consumer in such a way that your application supports an arbitrary number of consumers. The consumers are expected to consume at different rates, so consumers should not simply consume a predetermined number of Strings and then quit; one consumer may complete significantly earlier than the others and would be idle waiting for the other processes in this case. As such, youll want a way for your producer to signal its completion to the consumers. Youll also need to ensure that you dont have a consumer potentially waiting indefinitely for a String to be produced after the producer has completed (hint: see the LinkedBlockingQueue poll() methodthat allows a timeout). Well discuss this in more detail in class.
4. [10] Use a java.util.concurrent.ExecutorService to execute your producer and at least two consumers concurrently. Shut down the service after starting the two processes.
5. [20] Write code that is clear and efficient. Specifically, your code should be indented with respect to code blocks, avoid unnecessarily repetitive code, avoid code that is commented out or otherwise unused, use descriptive and consistent class/method/variable names, etc.
6. [+10] (Extra credit) Use a synchronized LinkedList in part 1 rather than aLinkedBlockingQueue. Youll then need to use the Object wait() method in part 2 to halt the producer process and an Object notify() or notifyAll() method in part 3 to resume the producer.The following is output for producing 10000 Strings with a queue of size 1000 and 2 consumers:produced: 1000produced: 2000consumer 2 consumed: 1000produced: 3000consumer 1 consumed: 1000produced: 4000consumer 2 consumed: 2000produced: 5000consumer 1 consumed: 2000produced: 6000consumer 2 consumed: 3000produced: 7000consumer 1 consumed: 3000produced: 8000consumer 2 consumed: 4000produced: 9000consumer 1 consumed: 4000produced: 10000done producing! 10000 producedconsumer 2 consumed: 5000consumer 1 done consuming! 4968 consumedconsumer 1 max String: fff60361-d394-46c7-92e4-ce7935da1518consumer 2 done consuming! 5032 consumedconsumer 2 max String: fffd0f28-e265-4e89-ad1a-8ac4a8281501Zip the Assignment7 folder in your Eclipse workspace directory, name the .zip file
Reviews
There are no reviews yet.