|
@@ -31,12 +31,12 @@ public class MonkeyTypewriter {
|
31
|
31
|
Thread monkeyMichelle = new Thread(unsafeCopy, "[4]");
|
32
|
32
|
Thread monkeyHeather = new Thread(unsafeCopy, "[5]");
|
33
|
33
|
|
|
34
|
+ Thread[] unsafeThreads = {monkeySteve,monkeyGuido,monkeyJon,monkeyMichelle,monkeyHeather};
|
|
35
|
+
|
|
36
|
+ for(Thread t : unsafeThreads){
|
|
37
|
+ t.start();
|
|
38
|
+ }
|
34
|
39
|
|
35
|
|
- monkeySteve.start();
|
36
|
|
- monkeyGuido.start();
|
37
|
|
- monkeyJon.start();
|
38
|
|
- monkeyMichelle.start();
|
39
|
|
- monkeyHeather.start();
|
40
|
40
|
|
41
|
41
|
|
42
|
42
|
SafeCopier safeCopy = new SafeCopier(introduction);
|
|
@@ -47,11 +47,24 @@ public class MonkeyTypewriter {
|
47
|
47
|
Thread monkeyAmy = new Thread(safeCopy,"[9]");
|
48
|
48
|
Thread monkeyDan = new Thread(safeCopy,"[10]");
|
49
|
49
|
|
50
|
|
- monkeyJoe.start();
|
51
|
|
- monkeyVince.start();
|
52
|
|
- monkeyKibret.start();
|
53
|
|
- monkeyAmy.start();
|
54
|
|
- monkeyDan.start();
|
|
50
|
+ Thread[] safeThreads = {monkeyJoe,monkeyVince,monkeyKibret,monkeyAmy,monkeyDan};
|
|
51
|
+ long startTime = System.currentTimeMillis();
|
|
52
|
+ for(Thread t : safeThreads){
|
|
53
|
+ t.start();
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ for(Thread t : safeThreads){
|
|
57
|
+ try {
|
|
58
|
+ t.join();
|
|
59
|
+ }
|
|
60
|
+ catch(InterruptedException e){
|
|
61
|
+ System.out.println("MONKEY INTERRUPTED");
|
|
62
|
+ }
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ long duration = System.currentTimeMillis()-startTime;
|
|
66
|
+
|
|
67
|
+
|
55
|
68
|
|
56
|
69
|
|
57
|
70
|
|
|
@@ -67,6 +80,10 @@ public class MonkeyTypewriter {
|
67
|
80
|
System.out.println("Unsafe Copy: \n" + unsafeCopy.copied);
|
68
|
81
|
System.out.println();
|
69
|
82
|
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+ System.out.println("Multi-threaded process computed in " + duration + "ms");
|
70
|
87
|
System.out.println("Safe Copy: \n" + safeCopy.copied);
|
71
|
88
|
}
|
72
|
89
|
}
|