|
@@ -1,7 +1,12 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Array;
|
|
4
|
+import java.util.ArrayList;
|
|
5
|
+import java.util.List;
|
|
6
|
+
|
3
|
7
|
public class MonkeyTypewriter {
|
4
|
8
|
public static void main(String[] args) {
|
|
9
|
+
|
5
|
10
|
String introduction = "It was the best of times,\n" +
|
6
|
11
|
"it was the blurst of times,\n" +
|
7
|
12
|
"it was the age of wisdom,\n" +
|
|
@@ -20,19 +25,27 @@ public class MonkeyTypewriter {
|
20
|
25
|
"its noisiest authorities insisted on its being received, for good or for\n" +
|
21
|
26
|
"evil, in the superlative degree of comparison only.";
|
22
|
27
|
|
23
|
|
- // Do all of the Monkey / Thread building here
|
24
|
|
- // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
|
25
|
|
- // A Tale Of Two Cities.
|
|
28
|
+ UnsafeCopier unsafeMonkey = new UnsafeCopier(introduction);
|
|
29
|
+ Thread thread[] = new Thread[5];
|
|
30
|
+ for(int i = 0; i < 5; i++) {
|
|
31
|
+ thread[i] = new Thread(unsafeMonkey);
|
|
32
|
+ thread[i].start();
|
|
33
|
+ }
|
26
|
34
|
|
|
35
|
+ SafeCopier safeMonkey = new SafeCopier(introduction);
|
|
36
|
+ Thread thread1[] = new Thread[5];
|
|
37
|
+ for(int i = 0; i < 5; i++) {
|
|
38
|
+ thread1[i] = new Thread(safeMonkey);
|
|
39
|
+ thread1[i].start();
|
|
40
|
+ }
|
27
|
41
|
|
28
|
|
- // This wait is here because main is still a thread and we want the main method to print the finished copies
|
29
|
|
- // after enough time has passed.
|
30
|
42
|
try {
|
31
|
43
|
Thread.sleep(1000);
|
32
|
44
|
} catch(InterruptedException e) {
|
33
|
45
|
System.out.println("MAIN INTERRUPTED");
|
34
|
46
|
}
|
35
|
47
|
|
36
|
|
- // Print out the copied versions here.
|
|
48
|
+ System.out.println("UnsafeMonkey: " + unsafeMonkey.copied);
|
|
49
|
+ System.out.println("SafeMonkey: " + safeMonkey.copied);
|
37
|
50
|
}
|
38
|
51
|
}
|