shakila 6 年前
父节点
当前提交
ba9762f3be

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

@@ -23,6 +23,35 @@ 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 monkey1 = new Thread(unsafeCopier);
28
+        Thread monkey2 = new Thread(unsafeCopier);
29
+        Thread monkey3 = new Thread(unsafeCopier);
30
+        Thread monkey4 = new Thread(unsafeCopier);
31
+        Thread monkey5 = new Thread(unsafeCopier);
32
+
33
+        monkey1.start();
34
+        monkey2.start();
35
+        monkey3.start();
36
+        monkey4.start();
37
+        monkey5.start();
38
+
39
+        SafeCopier safeCopier = new SafeCopier(introduction);
40
+        Thread safeMonkey1 = new Thread(safeCopier);
41
+        Thread safeMonkey2 = new Thread(safeCopier);
42
+        Thread safeMonkey3 = new Thread(safeCopier);
43
+        Thread safeMonkey4 = new Thread(safeCopier);
44
+        Thread safeMonkey5 = new Thread(safeCopier);
45
+
46
+        safeMonkey1.start();
47
+        safeMonkey2.start();
48
+        safeMonkey3.start();
49
+        safeMonkey4.start();
50
+        safeMonkey5.start();
51
+
52
+
53
+
54
+       //unsafeCopier.run();
26 55
 
27 56
 
28 57
         // This wait is here because main is still a thread and we want the main method to print the finished copies
@@ -34,5 +63,9 @@ public class MonkeyTypewriter {
34 63
         }
35 64
 
36 65
         // Print out the copied versions here.
66
+        System.out.println(unsafeCopier.copied);
67
+        System.out.println("==========================================================================");
68
+        System.out.println(safeCopier.copied);
69
+
37 70
     }
38 71
 }

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

@@ -1,8 +1,30 @@
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
+    Lock monkeyLock = new ReentrantLock();
13
+
14
+    public SafeCopier(String toCopy) {
15
+        super(toCopy);
16
+    }
17
+
18
+
19
+    @Override
20
+    public void run() {
21
+        while(stringIterator.hasNext()){    //true until there are no more words
22
+            monkeyLock.lock();
23
+            System.out.println(Thread.currentThread().getName());
24
+            copied += stringIterator.next() + " ";
25
+            monkeyLock.unlock();
26
+
27
+        }
28
+
29
+    }
8 30
 }

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

@@ -6,9 +6,17 @@ package io.zipcoder;
6 6
 public class UnsafeCopier extends Copier {
7 7
 
8 8
     public UnsafeCopier(String toCopy) {
9
+
9 10
         super(toCopy);
10 11
     }
11 12
 
13
+    @Override
12 14
     public void run() {
15
+        //while loop
16
+        while(stringIterator.hasNext()){    //true until there are no more words
17
+            copied += stringIterator.next() + " ";
18
+            //System.out.println(copied);
19
+        }
20
+
13 21
     }
14 22
 }