26.7k views
5 votes
Create a PostalWorkerThread class that extends Thread, which will serve the customers in line. The PostalWorkerThread class will need to acquire the postalWorkerMutex semaphore to enter the critical section where it can interact with the customer threads. Once a postal worker thread finishes serving a customer, it should release the postalWorkerMutex semaphore. You also need to ensure that only one postal worker can use the scales at a time. Currently, my code creates a semaphore for scalesMutex, but it is not being used in the code. You should also add print statements to my code to match the output format mentioned in the project description.

User Techdreams
by
9.4k points

1 Answer

3 votes

Final answer:

To create a PostalWorkerThread class that extends Thread and implements the required functionalities, you can follow these steps: Define the class and extend Thread, acquire and release the postalWorkerMutex semaphore, include and use the scalesMutex semaphore, and add print statements to match the specified output format.

Step-by-step explanation:

PostalWorkerThread class

To create a PostalWorkerThread class that extends Thread, you can define the class as follows:

public class PostalWorkerThread extends Thread {
// class implementation
}

Acquiring and Releasing Semaphores

To acquire the postalWorkerMutex semaphore and enter the critical section, you can use the acquire() method:

postalWorkerMutex.acquire();
// critical section
postalWorkerMutex.release();

Using scalesMutex Semaphore

Include the scalesMutex semaphore in your code by acquiring and releasing it in a similar manner:

scalesMutex.acquire();
// code for using the scales
scalesMutex.release();

Adding Print Statements

Use print statements to match the output format mentioned in the project description. For example:

System.out.println("Postal worker serving customer: " + customerId);

User Juanito Fatas
by
7.8k points