Tennessee Gibbs 6 jaren geleden
bovenliggende
commit
ec6abc0a53

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

@@ -20,13 +20,43 @@ public class MonkeyTypewriter {
20 20
                 "its noisiest authorities insisted on its being received, for good or for\n" +
21 21
                 "evil, in the superlative degree of comparison only.";
22 22
 
23
+
24
+        UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
23 25
         // Do all of the Monkey / Thread building here
26
+        Thread monkey1 = new Thread( unsafeCopier );
27
+        Thread monkey2 = new Thread( unsafeCopier );
28
+        Thread monkey3 = new Thread( unsafeCopier );
29
+        Thread monkey4 = new Thread( unsafeCopier );
30
+        Thread monkey5 = new Thread( unsafeCopier );
24 31
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 32
         // A Tale Of Two Cities.
26 33
 
27 34
 
35
+
36
+
37
+        monkey1.start();
38
+        monkey2.start();
39
+        monkey3.start();
40
+        monkey4.start();
41
+        monkey5.start();
28 42
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 43
         // after enough time has passed.
44
+        SafeCopier safeCopier = new SafeCopier(introduction);
45
+
46
+        Thread safemonkey1 = new Thread( safeCopier );
47
+        Thread safemonkey2 = new Thread( safeCopier );
48
+        Thread safemonkey3 = new Thread( safeCopier );
49
+        Thread safemonkey4 = new Thread( safeCopier );
50
+        Thread safemonkey5 = new Thread( safeCopier );
51
+
52
+
53
+        safemonkey1.start();
54
+        safemonkey2.start();
55
+        safemonkey3.start();
56
+        safemonkey4.start();
57
+        safemonkey5.start();
58
+
59
+        System.out.println();
30 60
         try {
31 61
             Thread.sleep(1000);
32 62
         } catch(InterruptedException e) {
@@ -34,5 +64,8 @@ public class MonkeyTypewriter {
34 64
         }
35 65
 
36 66
         // Print out the copied versions here.
67
+        System.out.println(unsafeCopier.copied);
68
+        System.out.println("======================================================");
69
+        System.out.println(safeCopier.copied);
37 70
     }
38 71
 }

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

@@ -1,8 +1,30 @@
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
+
15
+    public SafeCopier(String toCopy){
16
+        super(toCopy);
17
+
18
+
19
+    }
20
+
21
+    public void run() {
22
+        while (stringIterator.hasNext()){
23
+        monkeyLock.lock();
24
+        System.out.println(Thread.currentThread().getName());
25
+        copied += stringIterator.next() + " ";
26
+        monkeyLock.unlock();
27
+    }
28
+    }
29
+
8 30
 }

+ 11
- 2
src/main/java/io/zipcoder/UnsafeCopier.java Bestand weergeven

@@ -8,7 +8,16 @@ public class UnsafeCopier extends Copier {
8 8
     public UnsafeCopier(String toCopy) {
9 9
         super(toCopy);
10 10
     }
11
-
11
+@Override
12 12
     public void run() {
13
+        //while loop
14
+        while (stringIterator.hasNext()) {
15
+
16
+            //monkeyLock.lock();
17
+            copied += stringIterator.next() + " ";
18
+           // monkeyLock.unlock();
19
+
20
+        }
21
+        }
13 22
     }
14
-}
23
+