|
@@ -1,9 +1,11 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-public class MonkeyTypewriter {
|
4
|
|
- public static void main(String[] args) {
|
|
3
|
+
|
|
4
|
+public class MonkeyTypewriter{
|
|
5
|
+
|
|
6
|
+ public static void main(String[] args) throws InterruptedException {
|
5
|
7
|
String introduction = "It was the best of times,\n" +
|
6
|
|
- "it was the blurst of times,\n" +
|
|
8
|
+ "it was the worst of times,\n" +
|
7
|
9
|
"it was the age of wisdom,\n" +
|
8
|
10
|
"it was the age of foolishness,\n" +
|
9
|
11
|
"it was the epoch of belief,\n" +
|
|
@@ -20,9 +22,32 @@ public class MonkeyTypewriter {
|
20
|
22
|
"its noisiest authorities insisted on its being received, for good or for\n" +
|
21
|
23
|
"evil, in the superlative degree of comparison only.";
|
22
|
24
|
|
|
25
|
+
|
|
26
|
+
|
23
|
27
|
// Do all of the Monkey / Thread building here
|
24
|
28
|
// For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
|
25
|
|
- // A Tale Of Two Cities.
|
|
29
|
+ // A Tale Of Two Cities by Charles Dickens.
|
|
30
|
+
|
|
31
|
+ UnsafeCopier notSafe = new UnsafeCopier(introduction);
|
|
32
|
+ Thread[] notSafeThreads = new Thread[5];
|
|
33
|
+ for(int i=0; i<notSafeThreads.length; i++){
|
|
34
|
+ notSafeThreads[i] = new Thread(notSafe);
|
|
35
|
+ notSafeThreads[i].start();
|
|
36
|
+ }
|
|
37
|
+ for(Thread threads: notSafeThreads){
|
|
38
|
+ threads.join();
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+ SafeCopier safe = new SafeCopier(introduction);
|
|
43
|
+ Thread[] safeThread = new Thread[5];
|
|
44
|
+ for(int i = 0; i<safeThread.length; i++){
|
|
45
|
+ safeThread[i] = new Thread(safe);
|
|
46
|
+ safeThread[i].start();
|
|
47
|
+ }
|
|
48
|
+ for(Thread threads: safeThread){
|
|
49
|
+ threads.join();
|
|
50
|
+ }
|
26
|
51
|
|
27
|
52
|
|
28
|
53
|
// This wait is here because main is still a thread and we want the main method to print the finished copies
|
|
@@ -34,5 +59,11 @@ public class MonkeyTypewriter {
|
34
|
59
|
}
|
35
|
60
|
|
36
|
61
|
// Print out the copied versions here.
|
|
62
|
+ System.out.println(notSafe.copied);
|
|
63
|
+ System.out.println("____________________________________________");
|
|
64
|
+ System.out.println(safe.copied);
|
|
65
|
+
|
|
66
|
+
|
37
|
67
|
}
|
|
68
|
+
|
38
|
69
|
}
|