Mitch Taylor 6 vuotta sitten
vanhempi
commit
aadfd75913

+ 1
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java Näytä tiedosto

@@ -76,6 +76,7 @@ public class MonkeyTypewriter {
76 76
 
77 77
         // Print out the copied versions here.
78 78
         unsafeOutput(unsafeCopier.copied);
79
+        System.out.println();
79 80
         safeOutput(safeCopier.copied);
80 81
     }
81 82
 

+ 6
- 6
src/main/java/io/zipcoder/SafeCopier.java Näytä tiedosto

@@ -9,18 +9,18 @@ import java.util.concurrent.locks.ReentrantLock;
9 9
  */
10 10
 public class SafeCopier extends Copier {
11 11
 
12
-    Lock sharedLock = new ReentrantLock();
13
-
14 12
     public SafeCopier(String toCopy) {
15 13
         super(toCopy);
16 14
     }
17 15
 
18 16
     public void run() {
19
-        sharedLock.lock();
20
-        if (stringIterator.hasNext()) {
21
-            copied += stringIterator.next() + " ";
17
+        while (stringIterator.hasNext()) {
18
+            synchronized (stringIterator) {
19
+                if (stringIterator.hasNext()) {
20
+                    copied += stringIterator.next() + " ";
21
+                }
22
+            }
22 23
         }
23
-        sharedLock.unlock();
24 24
     }
25 25
 
26 26
 }

+ 4
- 2
src/main/java/io/zipcoder/UnsafeCopier.java Näytä tiedosto

@@ -10,8 +10,10 @@ public class UnsafeCopier extends Copier {
10 10
     }
11 11
 
12 12
     public void run() {
13
-        if (stringIterator.hasNext()) {
14
-            copied += stringIterator.next() + " ";
13
+        while (stringIterator.hasNext()) {
14
+            if (stringIterator.hasNext()) {
15
+                copied += stringIterator.next() + " ";
16
+            }
15 17
         }
16 18
     }
17 19