Bladeren bron

all the blurst monkeys at work

Allison Ziegler 6 jaren geleden
bovenliggende
commit
de577fbca6

+ 17
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java Bestand weergeven

23
         // Do all of the Monkey / Thread building here
23
         // Do all of the Monkey / Thread building here
24
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
24
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25
         // A Tale Of Two Cities.
25
         // A Tale Of Two Cities.
26
+        UnsafeCopier dangerWillRobinson = new UnsafeCopier(introduction);
27
+        new Thread(dangerWillRobinson).start();
28
+        new Thread(dangerWillRobinson).start();
29
+        new Thread(dangerWillRobinson).start();
30
+        new Thread(dangerWillRobinson).start();
31
+        new Thread(dangerWillRobinson).start();
32
+
33
+        SafeCopier safetyFirst = new SafeCopier(introduction);
34
+        new Thread(safetyFirst).start();
35
+        new Thread(safetyFirst).start();
36
+        new Thread(safetyFirst).start();
37
+        new Thread(safetyFirst).start();
38
+        new Thread(safetyFirst).start();
26
 
39
 
27
 
40
 
28
         // This wait is here because main is still a thread and we want the main method to print the finished copies
41
         // This wait is here because main is still a thread and we want the main method to print the finished copies
34
         }
47
         }
35
 
48
 
36
         // Print out the copied versions here.
49
         // Print out the copied versions here.
50
+        System.out.println("Unsafe Copier: ");
51
+        System.out.println(dangerWillRobinson.copied);
52
+        System.out.println("Safe Copier: ");
53
+        System.out.println(safetyFirst.copied);
37
     }
54
     }
38
 }
55
 }

+ 25
- 1
src/main/java/io/zipcoder/SafeCopier.java Bestand weergeven

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

+ 3
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Bestand weergeven

10
     }
10
     }
11
 
11
 
12
     public void run() {
12
     public void run() {
13
+        while (stringIterator.hasNext()) {
14
+            copied += stringIterator.next() + " ";
15
+        }
13
     }
16
     }
14
 }
17
 }