Karousha Fennimore 6 lat temu
rodzic
commit
b0d51082a8

+ 7
- 13
src/main/java/io/zipcoder/MonkeyTypewriter.java Wyświetl plik

@@ -28,12 +28,12 @@ public class MonkeyTypewriter {
28 28
         //created threads to avoid making 20 lines
29 29
         UnsafeCopier badCopier = new UnsafeCopier(introduction);
30 30
         SafeCopier safeCopier = new SafeCopier(introduction);
31
-        Thread[] badCopierMonkeys = new Thread[1];
32
-        Thread[] safeCopierMonkeys = new Thread[1];
31
+        Thread[] badCopierMonkeys = new Thread[5];
32
+        Thread[] safeCopierMonkeys = new Thread[5];
33 33
 
34
-        int count = 0;
35 34
 
36 35
         while (safeCopier.stringIterator.hasNext() || badCopier.stringIterator.hasNext()) {
36
+
37 37
             //creating new monkeys...rehabilitating the old ones and releasing them into the wild.
38 38
             for (int i = 0; i < badCopierMonkeys.length; i++) {
39 39
                 badCopierMonkeys[i] = new Thread(badCopier);
@@ -41,15 +41,8 @@ public class MonkeyTypewriter {
41 41
 
42 42
                 safeCopierMonkeys[i] = new Thread(safeCopier);
43 43
                 safeCopierMonkeys[i].start();
44
-
45
-                count ++;
46
-//            }
47
-//            for (Thread workerMonkeys : badCopierMonkeys) {
48
-//            }
49
-//            for (Thread workerMonkeys : safeCopierMonkeys) {
50
-//            }
44
+                
51 45
             }
52
-        }
53 46
             // This wait is here because main is still a thread and we want the main method to print the finished copies
54 47
             // after enough time has passed.
55 48
             try {
@@ -60,8 +53,9 @@ public class MonkeyTypewriter {
60 53
 
61 54
             System.out.println("Unsafe Copier: " + badCopier.copied + "\n" + "---------------");
62 55
             System.out.println("Safe Copier: " + safeCopier.copied);
63
-            System.out.println(count);
56
+
57
+
64 58
             // Print out the copied versions here.
65 59
         }
66
-
60
+    }
67 61
 }

+ 1
- 0
src/main/java/io/zipcoder/SafeCopier.java Wyświetl plik

@@ -23,6 +23,7 @@ public class SafeCopier extends Copier {
23 23
         //not an IF statement because the if statement kills the monkey
24 24
         //and we are not in the monkey killing business
25 25
         while (stringIterator.hasNext()) {
26
+            sharedLock.lock();
26 27
             copied += stringIterator.next() + " ";
27 28
         }
28 29
         sharedLock.unlock();

+ 144
- 0
src/test/java/io/zipcoder/SafeCopierTest.java Wyświetl plik

@@ -63,5 +63,149 @@ public class SafeCopierTest {
63 63
         //Then
64 64
         Assert.assertEquals(expected, actual);
65 65
     }
66
+    @Test
67
+    public void safeCopyTest2() {
68
+        //Given BEFORE
69
+
70
+        //When
71
+        monkey1.start();
72
+        monkey2.start();
73
+        monkey3.start();
74
+        monkey4.start();
75
+        monkey5.start();
76
+
77
+        String expected = introduction;
78
+        String actual = sc.copied;
79
+        //Then
80
+        Assert.assertEquals(expected, actual);
81
+    }
82
+    @Test
83
+    public void safeCopyTest3() {
84
+        //Given BEFORE
85
+
86
+        //When
87
+        monkey1.start();
88
+        monkey2.start();
89
+        monkey3.start();
90
+        monkey4.start();
91
+        monkey5.start();
92
+
93
+        String expected = introduction;
94
+        String actual = sc.copied;
95
+        //Then
96
+        Assert.assertEquals(expected, actual);
97
+    }
98
+    @Test
99
+    public void safeCopyTest4() {
100
+        //Given BEFORE
101
+
102
+        //When
103
+        monkey1.start();
104
+        monkey2.start();
105
+        monkey3.start();
106
+        monkey4.start();
107
+        monkey5.start();
108
+
109
+        String expected = introduction;
110
+        String actual = sc.copied;
111
+        //Then
112
+        Assert.assertEquals(expected, actual);
113
+    }
114
+    @Test
115
+    public void safeCopyTest5() {
116
+        //Given BEFORE
117
+
118
+        //When
119
+        monkey1.start();
120
+        monkey2.start();
121
+        monkey3.start();
122
+        monkey4.start();
123
+        monkey5.start();
124
+
125
+        String expected = introduction;
126
+        String actual = sc.copied;
127
+        //Then
128
+        Assert.assertEquals(expected, actual);
129
+    }
130
+    @Test
131
+    public void safeCopyTest6() {
132
+        //Given BEFORE
133
+
134
+        //When
135
+        monkey1.start();
136
+        monkey2.start();
137
+        monkey3.start();
138
+        monkey4.start();
139
+        monkey5.start();
140
+
141
+        String expected = introduction;
142
+        String actual = sc.copied;
143
+        //Then
144
+        Assert.assertEquals(expected, actual);
145
+    }
146
+    @Test
147
+    public void safeCopyTest7() {
148
+        //Given BEFORE
149
+
150
+        //When
151
+        monkey1.start();
152
+        monkey2.start();
153
+        monkey3.start();
154
+        monkey4.start();
155
+        monkey5.start();
156
+
157
+        String expected = introduction;
158
+        String actual = sc.copied;
159
+        //Then
160
+        Assert.assertEquals(expected, actual);
161
+    }
162
+    @Test
163
+    public void safeCopyTest8() {
164
+        //Given BEFORE
165
+
166
+        //When
167
+        monkey1.start();
168
+        monkey2.start();
169
+        monkey3.start();
170
+        monkey4.start();
171
+        monkey5.start();
172
+
173
+        String expected = introduction;
174
+        String actual = sc.copied;
175
+        //Then
176
+        Assert.assertEquals(expected, actual);
177
+    }
178
+    @Test
179
+    public void safeCopyTest9() {
180
+        //Given BEFORE
181
+
182
+        //When
183
+        monkey1.start();
184
+        monkey2.start();
185
+        monkey3.start();
186
+        monkey4.start();
187
+        monkey5.start();
188
+
189
+        String expected = introduction;
190
+        String actual = sc.copied;
191
+        //Then
192
+        Assert.assertEquals(expected, actual);
193
+    }
194
+    @Test
195
+    public void safeCopyTest10() {
196
+        //Given BEFORE
197
+
198
+        //When
199
+        monkey1.start();
200
+        monkey2.start();
201
+        monkey3.start();
202
+        monkey4.start();
203
+        monkey5.start();
204
+
205
+        String expected = introduction;
206
+        String actual = sc.copied;
207
+        //Then
208
+        Assert.assertEquals(expected, actual);
209
+    }
66 210
 
67 211
 }