Browse Source

Merge 747e3ad9c59b3a53a3a82c3b64ead0197b281f8c into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

Keith Brinker 6 years ago
parent
commit
a3f40b45f8
No account linked to committer's email

+ 28
- 1
src/main/java/io/zipcoder/MonkeyTypewriter.java View File

@@ -23,6 +23,30 @@ 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 monkeyTyping1 = new Thread(unsafeCopier);
28
+        Thread monkeyTyping2 = new Thread(unsafeCopier);
29
+        Thread monkeyTyping3 = new Thread(unsafeCopier);
30
+        Thread monkeyTyping4 = new Thread(unsafeCopier);
31
+        Thread monkeyTyping5 = new Thread(unsafeCopier);
32
+        monkeyTyping1.start();
33
+        monkeyTyping2.start();
34
+        monkeyTyping3.start();
35
+        monkeyTyping4.start();
36
+        monkeyTyping5.start();
37
+
38
+        SafeCopier safeCopier = new SafeCopier(introduction);
39
+        Thread monkeyTyping6 = new Thread(safeCopier);
40
+        Thread monkeyTyping7 = new Thread(safeCopier);
41
+        Thread monkeyTyping8 = new Thread(safeCopier);
42
+        Thread monkeyTyping9 = new Thread(safeCopier);
43
+        Thread monkeyTyping10 = new Thread(safeCopier);
44
+        monkeyTyping6.start();
45
+        monkeyTyping7.start();
46
+        monkeyTyping8.start();
47
+        monkeyTyping9.start();
48
+        monkeyTyping10.start();
49
+
26 50
 
27 51
 
28 52
         // This wait is here because main is still a thread and we want the main method to print the finished copies
@@ -32,7 +56,10 @@ public class MonkeyTypewriter {
32 56
         } catch(InterruptedException e) {
33 57
             System.out.println("MAIN INTERRUPTED");
34 58
         }
35
-
36 59
         // Print out the copied versions here.
60
+        System.out.println("UNSAFE\n");
61
+        System.out.println(unsafeCopier.copied);
62
+        System.out.println("\nSAFE\n");
63
+        System.out.println(safeCopier.copied);
37 64
     }
38 65
 }

+ 21
- 1
src/main/java/io/zipcoder/SafeCopier.java View File

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

+ 4
- 0
src/main/java/io/zipcoder/UnsafeCopier.java View File

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