ソースを参照

Planet of the Apes

rjsmall90 6 年 前
コミット
79ad34e9b9
共有3 個のファイルを変更した68 個の追加2 個の削除を含む
  1. 41
    1
      src/main/java/io/zipcoder/MonkeyTypewriter.java
  2. 22
    1
      src/main/java/io/zipcoder/SafeCopier.java
  3. 5
    0
      src/main/java/io/zipcoder/UnsafeCopier.java

+ 41
- 1
src/main/java/io/zipcoder/MonkeyTypewriter.java ファイルの表示

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.org.apache.regexp.internal.REDebugCompiler;
4
+
3 5
 public class MonkeyTypewriter {
4 6
     public static void main(String[] args) {
5 7
         String introduction = "It was the best of times,\n" +
@@ -24,6 +26,40 @@ public class MonkeyTypewriter {
24 26
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 27
         // A Tale Of Two Cities.
26 28
 
29
+        UnsafeCopier unsafe = new UnsafeCopier(introduction);
30
+        SafeCopier safe = new SafeCopier(introduction);
31
+
32
+        Thread monkey = new Thread(unsafe);
33
+        Thread ape = new Thread(unsafe);
34
+        Thread orangutan = new Thread(unsafe);
35
+        Thread primate = new Thread(unsafe);
36
+        Thread octanarian = new Thread(unsafe);
37
+
38
+        monkey.start();
39
+        ape.start();
40
+        orangutan.start();
41
+        primate.start();
42
+        octanarian.start();
43
+
44
+        Thread King = new Thread(safe);
45
+        Thread Ceasar = new Thread(safe);
46
+        Thread Maurice = new Thread(safe);
47
+        Thread Koba = new Thread(safe);
48
+        Thread Red = new Thread(safe);
49
+
50
+        King.start();
51
+        Ceasar.start();
52
+        Maurice.start();
53
+        Koba.start();
54
+        Red.start();
55
+
56
+
57
+
58
+       // System.out.println("======================");
59
+
60
+
61
+
62
+
27 63
 
28 64
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 65
         // after enough time has passed.
@@ -32,7 +68,11 @@ public class MonkeyTypewriter {
32 68
         } catch(InterruptedException e) {
33 69
             System.out.println("MAIN INTERRUPTED");
34 70
         }
35
-
36 71
         // Print out the copied versions here.
72
+        System.out.println(unsafe.copied);
73
+
74
+        System.out.println("======================");
75
+
76
+        System.out.println(safe.copied);
37 77
     }
38 78
 }

+ 22
- 1
src/main/java/io/zipcoder/SafeCopier.java ファイルの表示

@@ -1,8 +1,29 @@
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
-public class SafeCopier {
10
+public class SafeCopier extends Copier{
11
+
12
+    Lock monkeyLock = new ReentrantLock();
13
+
14
+    public SafeCopier(String toCopy) {
15
+        super(toCopy);
16
+    }
17
+
18
+    public void run() {
19
+        while(stringIterator.hasNext()) { //true until no words left
20
+            monkeyLock.lock();
21
+            System.out.println(Thread.currentThread().getName());
22
+            copied += stringIterator.next() + " ";
23
+            monkeyLock.unlock();
24
+        }
25
+
26
+
27
+
28
+    }
8 29
 }

+ 5
- 0
src/main/java/io/zipcoder/UnsafeCopier.java ファイルの表示

@@ -10,5 +10,10 @@ public class UnsafeCopier extends Copier {
10 10
     }
11 11
 
12 12
     public void run() {
13
+        while (stringIterator.hasNext()) { //true until no words left
14
+            copied += stringIterator.next() + " ";
15
+            //System.out.println(copied);
16
+        }
13 17
     }
14 18
 }
19
+