|
@@ -1,5 +1,16 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+/**
|
|
4
|
+ * @TODO
|
|
5
|
+ * @Part1 - Extend copier in the unsafeCopier class
|
|
6
|
+ * - After that create a run method that will
|
|
7
|
+ * grab the next word and add it to the copy
|
|
8
|
+ * - Modify this class to create 5 threads using
|
|
9
|
+ * the unsafierCopier and start them
|
|
10
|
+ * @Part2 - Finish the safecopier class and call it from
|
|
11
|
+ * the main method along with the unsafecopier
|
|
12
|
+ */
|
|
13
|
+
|
3
|
14
|
public class MonkeyTypewriter {
|
4
|
15
|
public static void main(String[] args) {
|
5
|
16
|
String introduction = "It was the best of times,\n" +
|
|
@@ -23,16 +34,42 @@ public class MonkeyTypewriter {
|
23
|
34
|
// Do all of the Monkey / Thread building here
|
24
|
35
|
// For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
|
25
|
36
|
// A Tale Of Two Cities.
|
|
37
|
+ //Safe threads
|
|
38
|
+ SafeCopier sc = new SafeCopier(introduction); //contstructing safe/unsafe copies of the introduction
|
|
39
|
+ UnsafeCopier usc = new UnsafeCopier(introduction);
|
26
|
40
|
|
27
|
|
-
|
|
41
|
+ Thread monkeySafe1 = new Thread(sc);
|
|
42
|
+ Thread monkeySafe2 = new Thread(sc);
|
|
43
|
+ Thread monkeySafe3 = new Thread(sc);
|
|
44
|
+ Thread monkeySafe4 = new Thread(sc);
|
|
45
|
+ Thread monkeySafe5 = new Thread(sc);
|
|
46
|
+ //Unsafe threads
|
|
47
|
+ Thread monkeyUnsafe1 = new Thread(usc);
|
|
48
|
+ Thread monkeyUnsafe2 = new Thread(usc);
|
|
49
|
+ Thread monkeyUnsafe3 = new Thread(usc);
|
|
50
|
+ Thread monkeyUnsafe4 = new Thread(usc);
|
|
51
|
+ Thread monkeyUnsafe5 = new Thread(usc);
|
|
52
|
+ //For each one of these copiers, make 5 monkeys start copying the intro
|
|
53
|
+ monkeySafe1.start();
|
|
54
|
+ monkeySafe2.start();
|
|
55
|
+ monkeySafe3.start();
|
|
56
|
+ monkeySafe4.start();
|
|
57
|
+ monkeySafe5.start();
|
|
58
|
+ //start unsafe copies
|
|
59
|
+ monkeyUnsafe1.start();
|
|
60
|
+ monkeyUnsafe2.start();
|
|
61
|
+ monkeyUnsafe3.start();
|
|
62
|
+ monkeyUnsafe4.start();
|
|
63
|
+ monkeyUnsafe5.start();
|
28
|
64
|
// This wait is here because main is still a thread and we want the main method to print the finished copies
|
29
|
65
|
// after enough time has passed.
|
30
|
66
|
try {
|
31
|
|
- Thread.sleep(1000);
|
|
67
|
+ Thread.sleep(1000); //wait time between each thread request
|
32
|
68
|
} catch(InterruptedException e) {
|
33
|
69
|
System.out.println("MAIN INTERRUPTED");
|
34
|
70
|
}
|
35
|
|
-
|
36
|
71
|
// Print out the copied versions here.
|
|
72
|
+ System.out.println(sc.copied + "\n"); //printing will be exactly as expected since it is locked
|
|
73
|
+ System.out.println(usc.copied); //printing of this will have unpredictable behavior and results are not safe due to lack of lock
|
37
|
74
|
}
|
38
|
75
|
}
|