|
@@ -24,6 +24,37 @@ public class MonkeyTypewriter {
|
24
|
24
|
// For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
|
25
|
25
|
// A Tale Of Two Cities.
|
26
|
26
|
|
|
27
|
+ UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
|
|
28
|
+
|
|
29
|
+ Thread monkeyUnsafe1 = new Thread(unsafeCopier);
|
|
30
|
+ Thread monkeyUnsafe2 = new Thread(unsafeCopier);
|
|
31
|
+ Thread monkeyUnsafe3 = new Thread(unsafeCopier);
|
|
32
|
+ Thread monkeyUnsafe4 = new Thread(unsafeCopier);
|
|
33
|
+ Thread monkeyUnsafe5 = new Thread(unsafeCopier);
|
|
34
|
+
|
|
35
|
+ monkeyUnsafe1.start();
|
|
36
|
+ monkeyUnsafe2.start();
|
|
37
|
+ monkeyUnsafe3.start();
|
|
38
|
+ monkeyUnsafe4.start();
|
|
39
|
+ monkeyUnsafe5.start();
|
|
40
|
+
|
|
41
|
+ // creating new safeCopier object & passing string introduction as a parameter
|
|
42
|
+
|
|
43
|
+ SafeCopier safeCopier = new SafeCopier(introduction);
|
|
44
|
+
|
|
45
|
+ // creating 5 safe threads and passing the safe copier obj into their constructors
|
|
46
|
+ Thread monkeySafe1 = new Thread(safeCopier);
|
|
47
|
+ Thread monkeySafe2 = new Thread(safeCopier);
|
|
48
|
+ Thread monkeySafe3 = new Thread(safeCopier);
|
|
49
|
+ Thread monkeySafe4 = new Thread(safeCopier);
|
|
50
|
+ Thread monkeySafe5 = new Thread(safeCopier);
|
|
51
|
+
|
|
52
|
+ // we are starting the threads
|
|
53
|
+ monkeySafe1.start();
|
|
54
|
+ monkeySafe2.start();
|
|
55
|
+ monkeySafe3.start();
|
|
56
|
+ monkeySafe4.start();
|
|
57
|
+ monkeySafe5.start();
|
27
|
58
|
|
28
|
59
|
// This wait is here because main is still a thread and we want the main method to print the finished copies
|
29
|
60
|
// after enough time has passed.
|
|
@@ -34,5 +65,10 @@ public class MonkeyTypewriter {
|
34
|
65
|
}
|
35
|
66
|
|
36
|
67
|
// Print out the copied versions here.
|
|
68
|
+ System.out.println("Unsafe: \n");
|
|
69
|
+ System.out.println(unsafeCopier.copied);
|
|
70
|
+ System.out.println("\nSafe: \n");
|
|
71
|
+
|
|
72
|
+ System.out.println(safeCopier.copied);
|
37
|
73
|
}
|
38
|
74
|
}
|