#10 vincesima

Open
vincesima wants to merge 1 commits from vincesima/ZCW-MonkeysTypewritersOhMy:master into master

+ 17
- 3
src/main/java/io/zipcoder/MonkeyTypewriter.java View File

@@ -23,16 +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 unsafe = new UnsafeCopier(introduction);
27
+        new Thread(unsafe).start();
28
+        new Thread(unsafe).start();
29
+        new Thread(unsafe).start();
30
+        new Thread(unsafe).start();
31
+        new Thread(unsafe).start();
32
+
33
+        SafeCopier safe = new SafeCopier(introduction);
34
+        new Thread(safe).start();
35
+        new Thread(safe).start();
36
+        new Thread(safe).start();
37
+        new Thread(safe).start();
38
+        new Thread(safe).start();
39
+
26 40
 
27 41
 
28 42
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 43
         // after enough time has passed.
30 44
         try {
31 45
             Thread.sleep(1000);
32
-        } catch(InterruptedException e) {
46
+        } catch (InterruptedException e) {
33 47
             System.out.println("MAIN INTERRUPTED");
34 48
         }
35
-
36
-        // Print out the copied versions here.
49
+        System.out.println("Unsafe: " + unsafe.copied + "\n\n\n\n\n\n");
50
+        System.out.println("Safe: " + safe.copied);
37 51
     }
38 52
 }

+ 30
- 2
src/main/java/io/zipcoder/SafeCopier.java View File

@@ -1,8 +1,36 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.concurrent.locks.ReadWriteLock;
4
+import java.util.concurrent.locks.ReentrantReadWriteLock;
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 {
8
-}
10
+
11
+    public class SafeCopier extends Copier {
12
+
13
+        ReadWriteLock lock = new ReentrantReadWriteLock();
14
+
15
+        public SafeCopier(String toCopy) {
16
+            super(toCopy);
17
+        }
18
+
19
+        public void run() {
20
+            lock.writeLock().lock();
21
+            lock.readLock().lock();
22
+            try {
23
+                while (stringIterator.hasNext()) {
24
+                    copied += stringIterator.next() + " ";
25
+                }
26
+            } finally {
27
+                copied = copied.trim();
28
+                lock.writeLock().unlock();
29
+                lock.readLock().unlock();
30
+            }
31
+
32
+        }
33
+    }
34
+
35
+
36
+

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

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.Iterator;
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,10 @@ public class UnsafeCopier extends Copier {
10 12
     }
11 13
 
12 14
     public void run() {
15
+        while (stringIterator.hasNext()) {
16
+                        copied += stringIterator.next() + " ";
17
+                    }
18
+                copied = copied.trim();
19
+
13 20
     }
14 21
 }