|
@@ -1,7 +1,7 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
public class MonkeyTypewriter {
|
4
|
|
- public static void main(String[] args) {
|
|
4
|
+ public static void main(String[] args) throws InterruptedException {
|
5
|
5
|
String introduction = "It was the best of times,\n" +
|
6
|
6
|
"it was the blurst of times,\n" +
|
7
|
7
|
"it was the age of wisdom,\n" +
|
|
@@ -19,11 +19,27 @@ public class MonkeyTypewriter {
|
19
|
19
|
"in short, the period was so far like the present period, that some of\n" +
|
20
|
20
|
"its noisiest authorities insisted on its being received, for good or for\n" +
|
21
|
21
|
"evil, in the superlative degree of comparison only.";
|
22
|
|
-
|
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.
|
26
|
|
-
|
|
22
|
+ //unsafe threads
|
|
23
|
+ UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
|
|
24
|
+ Thread[] thread = new Thread[5];
|
|
25
|
+ for (int i = 0; i < thread.length; i++){
|
|
26
|
+ thread[i] = new Thread(unsafeCopier);
|
|
27
|
+ thread[i].start();
|
|
28
|
+ }
|
|
29
|
+ //make sure the background thread is run after the threads do their jobs first
|
|
30
|
+// for (Thread threads : thread){
|
|
31
|
+// threads.join();
|
|
32
|
+// }
|
|
33
|
+ //safe
|
|
34
|
+ SafeCopier safeCopier = new SafeCopier(introduction);
|
|
35
|
+ Thread[] safeThread = new Thread[5];
|
|
36
|
+ for (int i = 0; i < safeThread.length; i++){
|
|
37
|
+ safeThread[i] = new Thread(safeCopier);
|
|
38
|
+ safeThread[i].start();
|
|
39
|
+ }
|
|
40
|
+// for (Thread safetyThreads : safeThread){
|
|
41
|
+// safetyThreads.join();
|
|
42
|
+// }
|
27
|
43
|
|
28
|
44
|
// This wait is here because main is still a thread and we want the main method to print the finished copies
|
29
|
45
|
// after enough time has passed.
|
|
@@ -32,7 +48,9 @@ public class MonkeyTypewriter {
|
32
|
48
|
} catch(InterruptedException e) {
|
33
|
49
|
System.out.println("MAIN INTERRUPTED");
|
34
|
50
|
}
|
35
|
|
-
|
36
|
51
|
// Print out the copied versions here.
|
|
52
|
+ System.out.println("Unsafe Monkeys: " + "\n" + unsafeCopier.copied);
|
|
53
|
+ System.out.println("----------------------------------------------");
|
|
54
|
+ System.out.println("Safe Monkeys: " + "\n" + safeCopier.copied);
|
37
|
55
|
}
|
38
|
56
|
}
|