Joshua Chung 6 лет назад
Родитель
Сommit
ace845649b

+ 19
- 6
src/main/java/io/zipcoder/MonkeyTypewriter.java Просмотреть файл

@@ -1,7 +1,12 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.List;
6
+
3 7
 public class MonkeyTypewriter {
4 8
     public static void main(String[] args) {
9
+
5 10
         String introduction = "It was the best of times,\n" +
6 11
                 "it was the blurst of times,\n" +
7 12
                 "it was the age of wisdom,\n" +
@@ -20,19 +25,27 @@ public class MonkeyTypewriter {
20 25
                 "its noisiest authorities insisted on its being received, for good or for\n" +
21 26
                 "evil, in the superlative degree of comparison only.";
22 27
 
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
25
-        // A Tale Of Two Cities.
28
+        UnsafeCopier unsafeMonkey = new UnsafeCopier(introduction);
29
+        Thread thread[] = new Thread[5];
30
+        for(int i = 0; i < 5; i++) {
31
+            thread[i] = new Thread(unsafeMonkey);
32
+            thread[i].start();
33
+        }
26 34
 
35
+        SafeCopier safeMonkey = new SafeCopier(introduction);
36
+        Thread thread1[] = new Thread[5];
37
+        for(int i = 0; i < 5; i++) {
38
+            thread1[i] = new Thread(safeMonkey);
39
+            thread1[i].start();
40
+        }
27 41
 
28
-        // This wait is here because main is still a thread and we want the main method to print the finished copies
29
-        // after enough time has passed.
30 42
         try {
31 43
             Thread.sleep(1000);
32 44
         } catch(InterruptedException e) {
33 45
             System.out.println("MAIN INTERRUPTED");
34 46
         }
35 47
 
36
-        // Print out the copied versions here.
48
+        System.out.println("UnsafeMonkey: " + unsafeMonkey.copied);
49
+        System.out.println("SafeMonkey: " + safeMonkey.copied);
37 50
     }
38 51
 }

+ 20
- 1
src/main/java/io/zipcoder/SafeCopier.java Просмотреть файл

@@ -1,8 +1,27 @@
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 {
10
+public class SafeCopier extends Copier{
11
+
12
+    public SafeCopier(String toCopy) {
13
+        super(toCopy);
14
+    }
15
+
16
+    public void run() {
17
+        ReadWriteLock rwl = new ReentrantReadWriteLock();
18
+        rwl.writeLock().lock();
19
+        rwl.readLock().lock();
20
+        try {
21
+            while(stringIterator.hasNext()) copied += stringIterator.next() + " ";
22
+        } finally {
23
+            rwl.writeLock().unlock();
24
+            rwl.readLock().unlock();
25
+        }
26
+    }
8 27
 }

+ 1
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Просмотреть файл

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