Selaa lähdekoodia

Merge 3b93fcfedfb2d936f0376e7aad4165a73da39bd5 into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

Katrice 6 vuotta sitten
vanhempi
commit
02f6a9a08f
No account linked to committer's email

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

@@ -23,7 +23,32 @@ public class MonkeyTypewriter {
23 23
         // Do all of the Monkey / Thread building here
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
+        Copier copier = new SafeCopier(introduction);
27
+        Thread thread1 = new Thread(copier);
28
+        Thread thread2 = new Thread(copier);
29
+        Thread thread3 = new Thread(copier);
30
+        Thread thread4 = new Thread(copier);
31
+        Thread thread5 = new Thread(copier);
26 32
 
33
+        Copier copiers = new UnsafeCopier(introduction);
34
+        Thread thread11 = new Thread(copiers);
35
+        Thread thread22 = new Thread(copiers);
36
+        Thread thread33 = new Thread(copiers);
37
+        Thread thread44 = new Thread(copiers);
38
+        Thread thread55 = new Thread(copiers);
39
+
40
+        thread1.start();
41
+        thread2.start();
42
+        thread3.start();
43
+        thread4.start();
44
+        thread5.start();
45
+
46
+
47
+        thread11.start();
48
+        thread22.start();
49
+        thread33.start();
50
+        thread44.start();
51
+        thread55.start();
27 52
 
28 53
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 54
         // after enough time has passed.
@@ -34,5 +59,8 @@ public class MonkeyTypewriter {
34 59
         }
35 60
 
36 61
         // Print out the copied versions here.
62
+        System.out.println(copier.copied);
63
+        System.out.println(copiers.copied);
64
+
37 65
     }
38 66
 }

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

@@ -1,8 +1,38 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.NoSuchElementException;
4
+import java.util.concurrent.locks.Lock;
5
+import java.util.concurrent.locks.ReentrantLock;
6
+
3 7
 /**
4 8
  * Make this extend the Copier like `UnsafeCopier`, except use locks to make sure that the actual intro gets printed
5 9
  * correctly every time.  Make the run method thread safe.
6 10
  */
7
-public class SafeCopier {
11
+public class SafeCopier extends Copier{
12
+    private Lock lock;
13
+
14
+    public SafeCopier(String toCopy) {
15
+        super(toCopy);
16
+        lock = new ReentrantLock();
17
+    }
18
+
19
+    public void run() {
20
+        try{
21
+            lock.lock();
22
+            copied += stringIterator.next() + " " + Thread.currentThread().getName()+ " ";
23
+        } catch (NoSuchElementException e){
24
+
25
+        } finally {
26
+            lock.unlock();
27
+        }
28
+    }
8 29
 }
30
+
31
+//Explanation/PseudoCode
32
+//created a private lock field with type lock
33
+//inorder to fully extend Copier had to override Copier constructor and methods
34
+//instantiated the lock (looked at the lecture for the "new ReentrantLock()"
35
+//try and catch for exception handling
36
+//NoSuchElementException  =  indicates that there are no more elements in the enumeration.
37
+//finally - The finally block always executes when the try block exits.
38
+//then I unlocked the lock

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

@@ -6,9 +6,26 @@ 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
+        }
13 17
     }
14 18
 }
19
+
20
+//PseudoCode
21
+//loop through the array in copier.java
22
+// monkey needs to grab the next word
23
+//append it to the copy
24
+//monkeytypewriter will create 5 monkey(threads) and use this class to start them
25
+
26
+//Ex of run method
27
+//tried a for loop initially and couldn't figure out what should be in the comparable part
28
+//so while stringIterator(which was turned into an arraylist in copier.java) knows our iteration
29
+//has more elements, then add the stringIterators next value to copied plus the space.
30
+
31
+