浏览代码

Merge 3ef00aae720ea1dccee3090f6082aa32ebcb8b9b into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

Anthony Jordan 6 年前
父节点
当前提交
5c668c264c
没有帐户链接到提交者的电子邮件

+ 20
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java 查看文件

@@ -23,6 +23,23 @@ public class MonkeyTypewriter {
23 23
         // Do all of the Monkey / Thread building here
24 24
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 25
         // A Tale Of Two Cities.
26
+        UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
27
+        Thread[] unsafeArray = new Thread[5];
28
+        for (int i =0; i < unsafeArray.length; i++){
29
+            unsafeArray[i] = new Thread(unsafeCopier);
30
+        }
31
+        for (Thread thread: unsafeArray) {
32
+            thread.start();
33
+        }
34
+
35
+        SafeCopier safeCopier = new SafeCopier(introduction);
36
+        Thread[] safeArray = new Thread[5];
37
+        for (int i = 0; i < safeArray.length; i++){
38
+            safeArray[i] = new Thread(safeCopier);
39
+        }
40
+        for (Thread thread: safeArray){
41
+            thread.start();
42
+        }
26 43
 
27 44
 
28 45
         // This wait is here because main is still a thread and we want the main method to print the finished copies
@@ -34,5 +51,8 @@ public class MonkeyTypewriter {
34 51
         }
35 52
 
36 53
         // Print out the copied versions here.
54
+        System.out.println(unsafeCopier.copied);
55
+        System.out.println("\n\n\n");
56
+        System.out.println(safeCopier.copied);
37 57
     }
38 58
 }

+ 21
- 1
src/main/java/io/zipcoder/SafeCopier.java 查看文件

@@ -1,8 +1,28 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.concurrent.locks.Lock;
4
+import java.util.concurrent.locks.ReentrantLock;
5
+
3 6
 /**
4 7
  * Make this extend the Copier like `UnsafeCopier`, except use locks to make sure that the actual intro gets printed
5 8
  * correctly every time.  Make the run method thread safe.
6 9
  */
7
-public class SafeCopier {
10
+public class SafeCopier extends Copier {
11
+
12
+    public SafeCopier(String toCopy) {
13
+        super(toCopy);
14
+    }
15
+
16
+    private Lock lock = new ReentrantLock();
17
+
18
+    public void run() {
19
+        while (stringIterator.hasNext()) {
20
+            lock.lock();
21
+            if (stringIterator.hasNext()) {
22
+                String word = stringIterator.next();
23
+                copied += word + " ";
24
+            }
25
+            lock.unlock();
26
+        }
27
+    }
8 28
 }

+ 8
- 0
src/main/java/io/zipcoder/UnsafeCopier.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.NoSuchElementException;
4
+
3 5
 /**
4 6
  * Modify the run function so that the monkeys each grab the next word and write it to the copy.
5 7
  */
@@ -10,5 +12,11 @@ public class UnsafeCopier extends Copier {
10 12
     }
11 13
 
12 14
     public void run() {
15
+        try {
16
+            while (stringIterator.hasNext()) {
17
+                String word = stringIterator.next();
18
+                copied += word + " ";
19
+            }
20
+        } catch (NoSuchElementException e) { }
13 21
     }
14 22
 }