Your cart is currently empty!
Note: In order to have your work on the extra points taken into account, you need to work on the regular points first; only working on the extra part without working on the regular part will lead to the 0 grade for this entire assignment. The goal of this assignment is to gain experience…
Note: In order to have your work on the extra points taken into account, you need to work on the regular points first; only working on the extra part without working on the regular part will lead to the 0 grade for this entire assignment.
The goal of this assignment is to gain experience working with concurrent programming via multiple threads and thread synchronization. You will write an implementation that simulates producer and consumer threads. The consumer will take more time to complete than the producer, causing the machine to run out of memory if threads are not synchronized in such a way that no more than a specified number of units are produced prior to being consumed.
You need to implement your program in such a way that it supports one producer and 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 others in this case. As such, you will want a way for your producer to signal its completion to all the consumers (e.g., creating a Boolean variable shared by the producer and all the consumers); you will also need to ensure that you don’t have a consumer potentially waiting indefinitely for a String to be produced (e.g., using LinkedBlockingQueue<T> poll() method with a timeout).
The following is an example output for 1 producer and 4 consumers, using the
LinkedBlockingQueue<T> implementation:
“Producer 1”: 100 events produced
“Producer 1”: 200 events produced
“Producer 1”: 300 events produced
“Producer 1”: 400 events produced
“Consumer 1”: 100 events consumed
“Consumer 2”: 100 events consumed
“Producer 1”: 500 events produced
“Consumer 4”: 100 events consumed
“Consumer 3”: 100 events consumed
“Producer 1”: 600 events produced
“Producer 1”: 700 events produced
“Producer 1”: 800 events produced
“Consumer 1”: 200 events consumed
“Consumer 3”: 200 events consumed
“Producer 1”: 900 events produced
“Consumer 4”: 200 events consumed
“Consumer 2”: 200 events consumed
“Producer 1”: 1000 events produced
Summary:
“Producer 1” produces 1000 events.
“Consumer 1” consumes 252 events.
“Consumer 2” consumes 246 events.
“Consumer 3” consumes 255 events.
“Consumer 4” consumes 247 events.
The following is an example output for 4 producers and 6 consumers, using the LinkedList<T> implementation:
“Producer 3”: 100 events produced
“Producer 4”: 100 events produced
“Producer 2”: 100 events produced
“Producer 1”: 100 events produced
“Consumer 2”: 100 events consumed
“Consumer 5”: 100 events consumed
“Consumer 6”: 100 events consumed
“Consumer 3”: 100 events consumed
“Consumer 4”: 100 events consumed
“Consumer 1”: 100 events consumed
“Producer 2”: 200 events produced
“Producer 4”: 200 events produced
“Producer 3”: 200 events produced
“Producer 1”: 200 events produced
Summary:
“Producer 1” produces 248 events.
“Producer 2” produces 253 events.
“Producer 3” produces 250 events.
“Producer 4” produces 249 events.
“Consumer 1” consumes 146 events.
“Consumer 2” consumes 178 events.
“Consumer 3” consumes 175 events.
“Consumer 4” consumes 167 events.
“Consumer 5” consumes 162 events.
“Consumer 6” consumes 172 events.
Note: If you choose not to work on the extra points, please use one folder, put your Java source file(s), i.e., .java file(s), into that folder, rename that folder as [YourName]Assignment7Regular, e.g., BillGatesAssignment7Regular, and then zip the folder into a single zipped file. If you choose to work on the extra points as well, in addition to the folder mentioned above, please have another folder, put your Java source file(s), i.e., .java file(s), on the extra points into that folder, rename that folder as [YourName]Assignment7Extra, e.g., BillGatesAssignment7Extra, and then select both folders and zip both of them altogether into a single zipped file. Finally, you rename your zipped file as [YourName]Assignment7.zip, e.g., BillGatesAssignment7.zip, and then upload it to Canvas. Please do not use the “package” keyword in any of your source file.