-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrintingSystem.java
executable file
·101 lines (82 loc) · 4.11 KB
/
PrintingSystem.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
/*
Author: Senthuran Ambalavanar | 2015215 | w1608452
*/
import utils.LoggerProcess;
import utils.Utility;
/**
* Represents the entire printing system, that is composed with a printer(resource), students, and technicians
*/
public class PrintingSystem {
public static void main(String[] args) {
// Create thread groups
ThreadGroup students = new ThreadGroup("students");
Utility.log(
LoggerProcess.PRINTING_SYSTEM,
"Created ThreadGroup: \"" + students.getName() + "\"",
null);
ThreadGroup technicians = new ThreadGroup("technicians");
Utility.log(
LoggerProcess.PRINTING_SYSTEM,
"Created ThreadGroup: \"" + technicians.getName() + "\"",
null);
// Create printer (resource)
LaserPrinter printer = new LaserPrinter("printer001", students);
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Initialised Printer", true);
// Create student threads
Student student1 = new Student("John", students, printer);
Utility.log(
LoggerProcess.PRINTING_SYSTEM,
"Initialised student: \"" + student1.getName() + "\"",
null);
Student student2 = new Student("Doe", students, printer);
Utility.log(
LoggerProcess.PRINTING_SYSTEM,
"Initialised student: \"" + student2.getName() + "\"",
null);
Student student3 = new Student("Tom", students, printer);
Utility.log(
LoggerProcess.PRINTING_SYSTEM,
"Initialised student: \"" + student3.getName() + "\"",
null);
Student student4 = new Student("Harry", students, printer);
Utility.log(
LoggerProcess.PRINTING_SYSTEM,
"Initialised student: \"" + student4.getName() + "\"",
null);
// Create technician threads
Technician paperTechnician = new PaperTechnician("PaperTechnician", technicians, printer);
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Initialised paper technician", null);
Technician tonerTechnician = new TonerTechnician("TonerTechnician", technicians, printer);
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Initialised toner technician", null);
// Start all the threads
student1.start();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Started student: \"" + student1.getName() + "\"", true);
student2.start();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Started student: \"" + student2.getName() + "\"", true);
student3.start();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Started student: \"" + student3.getName() + "\"", true);
student4.start();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Started student: \"" + student4.getName() + "\"", true);
paperTechnician.start();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Started paper technician", true);
tonerTechnician.start();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Started toner technician", true);
try {
student1.join();
Utility.log(LoggerProcess.PRINTING_SYSTEM, student1.getName() + " completed execution", true);
student2.join();
Utility.log(LoggerProcess.PRINTING_SYSTEM, student2.getName() + " completed execution", true);
student3.join();
Utility.log(LoggerProcess.PRINTING_SYSTEM, student3.getName() + " completed execution", true);
student4.join();
Utility.log(LoggerProcess.PRINTING_SYSTEM, student4.getName() + " completed execution", true);
paperTechnician.join();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Paper technician completed execution", true);
tonerTechnician.join();
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Toner technician completed execution", true);
Utility.log(LoggerProcess.PRINTING_SYSTEM, "Tasks completed. " + printer.toString(), true);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}