-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
193 lines (172 loc) · 5.32 KB
/
Main.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;
class Computer extends Thread{
ReentrantLock lock;
int computerID;
ArrayList<ArrayList<String>> printTasks;
SharedQueue queue;
public Computer(int computerID, SharedQueue queue, ArrayList<ArrayList<String>> printTasks){
this.computerID = computerID;
this.lock = new ReentrantLock();
this.queue = queue;
this.printTasks = printTasks;
}
public synchronized ArrayList<String> getPrintTask() throws InterruptedException {
while (this.printTasks.isEmpty()) {
wait();
}
ArrayList<String> printTask = printTasks.remove(0);
notifyAll();
return printTask;
}
public void run() {
while (true){
this.lock.lock();
try {
ArrayList<String> printTask = getPrintTask();
addPrintJob(printTask.get(0), printTask.get(1));
//Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public void addPrintJob(String fileName, String fileType){
if (fileType.equals("pdf")){
PrintJob printJob = new PrintJob(fileName, fileType);
if (!queue.isFull()){
queue.addPrintJob(printJob);
System.out.println("Computer "+this.computerID + " added "+ fileName +" to the shared queue.");
}else{
System.out.println("Cannot any more print jobs.");
}
}else{
try{
throw new TypeNotSupportedException("This file type is not supported.");
}catch(TypeNotSupportedException e){
System.out.println(e.getMessage());
}
}
}
}
class Printer extends Thread{
ReentrantLock lock;
int printerID;
SharedQueue queue;
public Printer(int printerID,SharedQueue queue){
this.printerID = printerID;
this.queue = queue;
this.lock = new ReentrantLock();
}
public synchronized PrintJob getPrintDetails() throws InterruptedException {
while (this.queue.getQueue().isEmpty()) {
wait();
}
PrintJob printDetails = queue.getQueue().remove(0);
notifyAll();
return printDetails;
}
public void run() {
while (true){
this.lock.lock();
try {
PrintJob printTask = getPrintDetails();
print(printTask);
Thread.sleep(10000);
}
catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public void print(PrintJob printJob){
this.lock.lock();
try{
if (printJob.getFileType().equals("pdf")){
System.out.println("Printer "+this.printerID+ " is printing "+printJob.getFileName());
}else{
try{
throw new TypeNotSupportedException("This file type is not supported.");
}catch(TypeNotSupportedException e){
System.out.println(e.getMessage());
}
}
}finally{
this.lock.unlock();
}
}
}
class PrintJob {
private String fileName;
private String fileType;
public PrintJob(String fileName, String fileType) {
this.fileName = fileName;
this.fileType = fileType;
}
public String getFileName() {
return fileName;
}
public String getFileType() {
return fileType;
}
}
class SharedQueue {
ArrayList<PrintJob> queue;
int capacity;
public SharedQueue(int capacity) {
this.queue = new ArrayList<>();
this.capacity = capacity;
}
public ArrayList<PrintJob> getQueue() {
return queue;
}
public void addPrintJob(PrintJob printJob) {
queue.add(printJob);
}
public boolean isFull(){
if (queue.size()<capacity){
return false;
}
return true;
}
}
class TypeNotSupportedException extends Exception{
public TypeNotSupportedException(String message){
super(message);
}
}
public class Main {
public static void main(String[] args) {
SharedQueue sharedQueue = new SharedQueue(5);
ArrayList<ArrayList<String>> printTasks = new ArrayList<>();
ArrayList<String>task1 = new ArrayList<>();
task1.add("test.pdf");
task1.add("pdf");
printTasks.add(task1);
ArrayList<String>task2 = new ArrayList<>();
task2.add("test2.png");
task2.add("png");
printTasks.add(task2);
ArrayList<String>task3 = new ArrayList<>();
task3.add("test3.pdf");
task3.add("pdf");
printTasks.add(task3);
ArrayList<String>task4 = new ArrayList<>();
task4.add("test4.pdf");
task4.add("pdf");
printTasks.add(task4);
for (int i = 1; i <= 3; i++) {
Computer computer = new Computer(i, sharedQueue, printTasks);
computer.start();
}
for (int i = 1; i <= 2; i++) {
Printer printer = new Printer(i, sharedQueue);
printer.start();
}
}
}