Luis Romero преди 6 години
родител
ревизия
7ddc59ec3d
променени са 3 файла, в които са добавени 51 реда и са изтрити 0 реда
  1. 33
    0
      src/main/java/io/zipcoder/MonkeyTypewriter.java
  2. 12
    0
      src/main/java/io/zipcoder/SafeCopier.java
  3. 6
    0
      src/main/java/io/zipcoder/UnsafeCopier.java

+ 33
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java Целия файл

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 public class MonkeyTypewriter {
4 6
 
5 7
     private String inputToCopy;
@@ -35,6 +37,34 @@ public class MonkeyTypewriter {
35 37
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
36 38
         // A Tale Of Two Cities.
37 39
 
40
+        int numberOfUnsafeMonkeys = 5;
41
+        int numberOfSafeMonkeys = 5;
42
+
43
+        // UNSAFE COPYING
44
+        UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
45
+        ArrayList<Thread> unsafeCopierThreads = new ArrayList<Thread>();
46
+
47
+        while (numberOfUnsafeMonkeys > 0) {
48
+            unsafeCopierThreads.add(new Thread(unsafeCopier));
49
+            numberOfUnsafeMonkeys--;
50
+        }
51
+
52
+        for (Thread unsafeCopierThread : unsafeCopierThreads) {
53
+            unsafeCopierThread.start();
54
+        }
55
+
56
+        // SAFE COPYING
57
+        SafeCopier safeCopier = new SafeCopier(introduction);
58
+        ArrayList<Thread> safeCopierThreads = new ArrayList<Thread>();
59
+
60
+        while (numberOfSafeMonkeys > 0) {
61
+            safeCopierThreads.add(new Thread(safeCopier));
62
+            numberOfSafeMonkeys--;
63
+        }
64
+
65
+        for (Thread safeCopierThread : safeCopierThreads) {
66
+            safeCopierThread.start();
67
+        }
38 68
 
39 69
         // This wait is here because main is still a thread and we want the main method to print the finished copies
40 70
         // after enough time has passed.
@@ -45,5 +75,8 @@ public class MonkeyTypewriter {
45 75
         }
46 76
 
47 77
         // Print out the copied versions here.
78
+        System.out.println(unsafeCopier.copied);
79
+        System.out.println("*************************************************");
80
+        System.out.println(safeCopier.copied);
48 81
     }
49 82
 }

+ 12
- 0
src/main/java/io/zipcoder/SafeCopier.java Целия файл

@@ -1,16 +1,28 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.concurrent.locks.Lock;
4
+import java.util.concurrent.locks.ReentrantLock;
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 10
 public class SafeCopier extends Copier {
8 11
 
12
+    Lock lock = new ReentrantLock();
13
+
9 14
     public SafeCopier(String toCopy) {
10 15
         super(toCopy);
11 16
     }
12 17
 
13 18
     public void run() {
19
+        StringBuilder sb = new StringBuilder();
14 20
 
21
+        lock.lock();
22
+        while (stringIterator.hasNext()) {
23
+            sb.append(this.stringIterator.next() + " ");
24
+            this.copied = sb.toString();
25
+        }
26
+        lock.unlock();
15 27
     }
16 28
 }

+ 6
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Целия файл

@@ -10,5 +10,11 @@ public class UnsafeCopier extends Copier {
10 10
     }
11 11
 
12 12
     public void run() {
13
+        StringBuilder sb = new StringBuilder();
14
+
15
+        while (this.stringIterator.hasNext()) {
16
+            sb.append(this.stringIterator.next() + " " + Thread.currentThread().getName() + " ");
17
+            this.copied = sb.toString();
18
+        }
13 19
     }
14 20
 }