-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedQueue.java
More file actions
32 lines (28 loc) · 1.03 KB
/
SharedQueue.java
File metadata and controls
32 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.LinkedList;
import java.util.Queue;
public class SharedQueue {
public final Queue<PrintJob> queue = new LinkedList<>();
private final int capacity = 5;
public synchronized void enqueue(PrintJob job, String computerID) throws InterruptedException {
while (queue.size() == capacity) {
System.out.println("Queue is Full. Wait until printers are free...");
wait();
}
System.out.println("Computer-" + computerID + " ---> Enqueued ---> " + job.getJobId());
queue.add(job);
notifyAll();
}
public synchronized PrintJob dequeue(int printerID) throws InterruptedException {
while (queue.isEmpty()) {
Thread.sleep(3000);
System.out.println("waiting for order...");
}
PrintJob job = queue.poll();
System.out.println("printer-" + printerID + " ---> Dequeuing ---> " + job.getJobId());
notifyAll();
return job;
}
public Queue<PrintJob> GetQueue() {
return this.queue;
}
}