Pārlūkot izejas kodu

Planet of the Apes

rjsmall90 6 gadus atpakaļ
vecāks
revīzija
79ad34e9b9

+ 41
- 1
src/main/java/io/zipcoder/MonkeyTypewriter.java Parādīt failu

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
+import com.sun.org.apache.regexp.internal.REDebugCompiler;
4
+
3
 public class MonkeyTypewriter {
5
 public class MonkeyTypewriter {
4
     public static void main(String[] args) {
6
     public static void main(String[] args) {
5
         String introduction = "It was the best of times,\n" +
7
         String introduction = "It was the best of times,\n" +
24
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
26
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25
         // A Tale Of Two Cities.
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
         // This wait is here because main is still a thread and we want the main method to print the finished copies
64
         // 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.
65
         // after enough time has passed.
32
         } catch(InterruptedException e) {
68
         } catch(InterruptedException e) {
33
             System.out.println("MAIN INTERRUPTED");
69
             System.out.println("MAIN INTERRUPTED");
34
         }
70
         }
35
-
36
         // Print out the copied versions here.
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 Parādīt failu

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
+import java.util.concurrent.locks.Lock;
4
+import java.util.concurrent.locks.ReentrantLock;
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
+    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 Parādīt failu

10
     }
10
     }
11
 
11
 
12
     public void run() {
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
+