David Thornley il y a 6 ans
Parent
révision
1016b9280f

+ 19
- 2
src/main/java/io/zipcoder/MonkeyTypewriter.java Voir le fichier

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

+ 31
- 1
src/main/java/io/zipcoder/SafeCopier.java Voir le fichier

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

+ 3
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Voir le fichier

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