Selaa lähdekoodia

Merge 893724fffedb53603a963bb666676bcb71a4bc60 into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

Jessica Campbell 6 vuotta sitten
vanhempi
commit
71a5ef0d29
No account linked to committer's email

+ 36
- 0
src/main/java/io/zipcoder/MonkeyTypewriter.java Näytä tiedosto

@@ -24,6 +24,37 @@ public class MonkeyTypewriter {
24 24
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 25
         // A Tale Of Two Cities.
26 26
 
27
+        UnsafeCopier unsafeCopier = new UnsafeCopier(introduction);
28
+
29
+        Thread monkeyUnsafe1 = new Thread(unsafeCopier);
30
+        Thread monkeyUnsafe2 = new Thread(unsafeCopier);
31
+        Thread monkeyUnsafe3 = new Thread(unsafeCopier);
32
+        Thread monkeyUnsafe4 = new Thread(unsafeCopier);
33
+        Thread monkeyUnsafe5 = new Thread(unsafeCopier);
34
+
35
+        monkeyUnsafe1.start();
36
+        monkeyUnsafe2.start();
37
+        monkeyUnsafe3.start();
38
+        monkeyUnsafe4.start();
39
+        monkeyUnsafe5.start();
40
+
41
+        // creating new safeCopier object & passing string introduction as a parameter
42
+
43
+        SafeCopier safeCopier = new SafeCopier(introduction);
44
+
45
+        // creating 5 safe threads and passing the safe copier obj into their constructors
46
+        Thread monkeySafe1 = new Thread(safeCopier);
47
+        Thread monkeySafe2 = new Thread(safeCopier);
48
+        Thread monkeySafe3 = new Thread(safeCopier);
49
+        Thread monkeySafe4 = new Thread(safeCopier);
50
+        Thread monkeySafe5 = new Thread(safeCopier);
51
+
52
+        // we are starting the threads
53
+        monkeySafe1.start();
54
+        monkeySafe2.start();
55
+        monkeySafe3.start();
56
+        monkeySafe4.start();
57
+        monkeySafe5.start();
27 58
 
28 59
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 60
         // after enough time has passed.
@@ -34,5 +65,10 @@ public class MonkeyTypewriter {
34 65
         }
35 66
 
36 67
         // Print out the copied versions here.
68
+        System.out.println("Unsafe: \n");
69
+        System.out.println(unsafeCopier.copied);
70
+        System.out.println("\nSafe: \n");
71
+
72
+        System.out.println(safeCopier.copied);
37 73
     }
38 74
 }

+ 32
- 1
src/main/java/io/zipcoder/SafeCopier.java Näytä tiedosto

@@ -1,8 +1,39 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.NoSuchElementException;
4
+import java.util.concurrent.TimeUnit;
5
+import java.util.concurrent.locks.Condition;
6
+import java.util.concurrent.locks.Lock;
7
+import java.util.concurrent.locks.ReentrantLock;
8
+
3 9
 /**
4 10
  * Make this extend the Copier like `UnsafeCopier`, except use locks to make sure that the actual intro gets printed
5 11
  * correctly every time.  Make the run method thread safe.
6 12
  */
7
-public class SafeCopier {
13
+public class SafeCopier extends Copier {
14
+
15
+    Lock sharedLock = new ReentrantLock();
16
+
17
+    public SafeCopier(String toCopy) {
18
+        super(toCopy);
19
+    }
20
+
21
+    public void run() {
22
+        try{
23
+            TimeUnit.MILLISECONDS.sleep(1000);
24
+        } catch (InterruptedException e){
25
+            e.printStackTrace();
26
+        }
27
+
28
+        while (stringIterator.hasNext()) {
29
+            sharedLock.lock();
30
+            try {
31
+                copied += stringIterator.next() + " " + Thread.currentThread().getName();
32
+            } catch (NoSuchElementException e) {
33
+
34
+            } finally {
35
+                sharedLock.unlock();
36
+            }
37
+        }
38
+    }
8 39
 }

+ 5
- 0
src/main/java/io/zipcoder/UnsafeCopier.java Näytä tiedosto

@@ -6,9 +6,14 @@ package io.zipcoder;
6 6
 public class UnsafeCopier extends Copier {
7 7
 
8 8
     public UnsafeCopier(String toCopy) {
9
+
9 10
         super(toCopy);
10 11
     }
11 12
 
12 13
     public void run() {
14
+        while(stringIterator.hasNext()){
15
+            copied += stringIterator.next() + " ";
16
+        }
17
+
13 18
     }
14 19
 }