Question: Hello, in Java, I'm suppose to create a program that uses threads and synchronization control. As a background: modern operating systems provide features for a

Hello, in Java, I'm suppose to create a program that uses threads and synchronization control.

As a background: modern operating systems provide features for a process to contain multiple threads of control and allow them to cooperate together. Manipulating shared data requres careful synchronization. Two widely used synchronization mechanisms are mutex lock (spinlock) and semaphore.

Goal for this program: In the classic producer-consumer problem, a producer thread inserts items into a buffer, and a consumer thread consumes items from the buffer simultaneously. The goal for this program is to use Java to solve a M producer-N consumer problem by generating n producer threads and M consumer threads to simuultaneously access teh buffer, and please use comments to explain what is written in the program.

This program should satisfy the following requirements:

1. The program should accept 4 input parameters, namely the number of producer threads, the number of consumer threads, the size of the buffer, and the number of items to be produced. For example, the following command starts your program with 4 producer threads, 5 consumer threads, a buffer of 10 items, and generate 1000 items in total as shown in bold.

$./producer-consumer 4 5 10 1000

2. Similar to the producer-consumer problem, a buffer is maintined in the program and shared among all producers and consumers. Each item in buffer is an integer.

3. Each producer thread generates a random integer each time, inserts it into the buffer, increments a counter, num_produced, which is shared by all producers to track how many items being created, and records the time and the item being produced in an event log. The event log should record the events in the order of timestamps.

4. Each consumer thread consumes the items of the buffer in the First-In-First-Out (FIFO) order, meaning that if item A is inserted into the buffer earlier than item B, then item A should be consumed earlier than item B, increments a counter, num_consumed, which is shared by all consumers to track how many blocks are consumed, and record the time and the item being consumed in an event log. The event log should record the events in the order of timestamps.

5. Use either mutex lock (spinlock) or semaphore to synchronize threads working on the shared resources in the program.

6. When the specified number of items are produced and consumed, the program should exit and generate the following output:

(i) print the total number of produced items, (ii) print the total number of consumed items, and (iii) output the even log files, named producer-event.log and consumder-event.log. Each record of the log file should be formatted as the following:

.

Basically, each record logs when and which thread produced an item into or consumed an item from an entry in the shared buffer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!