|
@@ -1,5 +1,7 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+
|
3
|
5
|
public class MonkeyTypewriter {
|
4
|
6
|
|
5
|
7
|
private String inputToCopy;
|
|
@@ -35,6 +37,34 @@ public class MonkeyTypewriter {
|
35
|
37
|
// For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
|
36
|
38
|
// A Tale Of Two Cities.
|
37
|
39
|
|
|
40
|
+ int numberOfUnsafeMonkeys = 5;
|
|
41
|
+ int numberOfSafeMonkeys = 5;
|
|
42
|
+
|
|
43
|
+ // UNSAFE COPYING
|
|
44
|
+ UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
|
|
45
|
+ ArrayList<Thread> unsafeCopierThreads = new ArrayList<Thread>();
|
|
46
|
+
|
|
47
|
+ while (numberOfUnsafeMonkeys > 0) {
|
|
48
|
+ unsafeCopierThreads.add(new Thread(unsafeCopier));
|
|
49
|
+ numberOfUnsafeMonkeys--;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ for (Thread unsafeCopierThread : unsafeCopierThreads) {
|
|
53
|
+ unsafeCopierThread.start();
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ // SAFE COPYING
|
|
57
|
+ SafeCopier safeCopier = new SafeCopier(introduction);
|
|
58
|
+ ArrayList<Thread> safeCopierThreads = new ArrayList<Thread>();
|
|
59
|
+
|
|
60
|
+ while (numberOfSafeMonkeys > 0) {
|
|
61
|
+ safeCopierThreads.add(new Thread(safeCopier));
|
|
62
|
+ numberOfSafeMonkeys--;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ for (Thread safeCopierThread : safeCopierThreads) {
|
|
66
|
+ safeCopierThread.start();
|
|
67
|
+ }
|
38
|
68
|
|
39
|
69
|
// This wait is here because main is still a thread and we want the main method to print the finished copies
|
40
|
70
|
// after enough time has passed.
|
|
@@ -45,5 +75,8 @@ public class MonkeyTypewriter {
|
45
|
75
|
}
|
46
|
76
|
|
47
|
77
|
// Print out the copied versions here.
|
|
78
|
+ System.out.println(unsafeCopier.copied);
|
|
79
|
+ System.out.println("*************************************************");
|
|
80
|
+ System.out.println(safeCopier.copied);
|
48
|
81
|
}
|
49
|
82
|
}
|