Follow these steps to implement the example:
- Create a class named PrintQueue that will implement the print queue:
public class PrintQueue {
- This class will have three private attributes. A semaphore named semaphore, an array of Booleans named freePrinters, and a lock named lockPrinters, as shown in the following code snippet:
private final Semaphore semaphore; private final boolean freePrinters[]; private final Lock lockPrinters;
- Implement the constructor of the class. It initializes the three attributes of the class, as shown in the following code snippet:
public PrintQueue(){ semaphore=new Semaphore(3); freePrinters=new boolean[3]; for (int i=0; i<3; i++){ freePrinters[i]=true; } lockPrinters=new ReentrantLock(); ...