Browse Source

Merge ed87c4eda1fb91a9a3e7d9df9def1b490f010431 into dbc7033eb3d6c33c19985384bea0b1a977cc30d3

SupaGrammer 6 years ago
parent
commit
07988ac208
No account linked to committer's email

+ 2
- 2
src/main/java/io/zipcoder/Copier.java View File

@@ -13,9 +13,9 @@ public abstract class Copier implements Runnable {
13 13
 
14 14
     public Copier(String toCopy) {
15 15
         // Take the input string, split it on spaces, turn that array to an arraylist, and then grab its iterator.
16
-        this.stringIterator = Arrays.asList(toCopy.split(" ")).iterator();
16
+        this.stringIterator = Arrays.asList(toCopy.split(" ")).iterator(); //going through each element as long as it exist (for loop?)
17 17
         this.copied = "";
18 18
     }
19 19
 
20
-    public abstract void run();
20
+    public abstract void run(); //this abstract class is basically telling us it must be used MUSTTTTTTTTT
21 21
 }

+ 40
- 3
src/main/java/io/zipcoder/MonkeyTypewriter.java View File

@@ -1,5 +1,16 @@
1 1
 package io.zipcoder;
2 2
 
3
+/**
4
+ * @TODO
5
+ * @Part1 - Extend copier in the unsafeCopier class
6
+ *        - After that create a run method that will
7
+ *          grab the next word and add it to the copy
8
+ *        - Modify this class to create 5 threads using
9
+ *          the unsafierCopier and start them
10
+ * @Part2 - Finish the safecopier class and call it from
11
+ *          the main method along with the unsafecopier
12
+ */
13
+
3 14
 public class MonkeyTypewriter {
4 15
     public static void main(String[] args) {
5 16
         String introduction = "It was the best of times,\n" +
@@ -23,16 +34,42 @@ public class MonkeyTypewriter {
23 34
         // Do all of the Monkey / Thread building here
24 35
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
25 36
         // A Tale Of Two Cities.
37
+        //Safe threads
38
+        SafeCopier sc = new SafeCopier(introduction); //contstructing safe/unsafe copies of the introduction
39
+        UnsafeCopier usc = new UnsafeCopier(introduction);
26 40
 
27
-
41
+        Thread monkeySafe1 = new Thread(sc);
42
+        Thread monkeySafe2 = new Thread(sc);
43
+        Thread monkeySafe3 = new Thread(sc);
44
+        Thread monkeySafe4 = new Thread(sc);
45
+        Thread monkeySafe5 = new Thread(sc);
46
+        //Unsafe threads
47
+        Thread monkeyUnsafe1 = new Thread(usc);
48
+        Thread monkeyUnsafe2 = new Thread(usc);
49
+        Thread monkeyUnsafe3 = new Thread(usc);
50
+        Thread monkeyUnsafe4 = new Thread(usc);
51
+        Thread monkeyUnsafe5 = new Thread(usc);
52
+        //For each one of these copiers, make 5 monkeys start copying the intro
53
+        monkeySafe1.start();
54
+        monkeySafe2.start();
55
+        monkeySafe3.start();
56
+        monkeySafe4.start();
57
+        monkeySafe5.start();
58
+        //start unsafe copies
59
+        monkeyUnsafe1.start();
60
+        monkeyUnsafe2.start();
61
+        monkeyUnsafe3.start();
62
+        monkeyUnsafe4.start();
63
+        monkeyUnsafe5.start();
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.
30 66
         try {
31
-            Thread.sleep(1000);
67
+            Thread.sleep(1000); //wait time between each thread request
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(sc.copied + "\n"); //printing will be exactly as expected since it is locked
73
+        System.out.println(usc.copied); //printing of this will have unpredictable behavior and results are not safe due to lack of lock
37 74
     }
38 75
 }

+ 19
- 2
src/main/java/io/zipcoder/SafeCopier.java View File

@@ -1,8 +1,25 @@
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 {
8
-}
10
+public class SafeCopier extends Copier{ //this will inherit the behavior of the abstract copier class
11
+
12
+    private Lock lock = new ReentrantLock();
13
+
14
+    public SafeCopier(String strCopy){
15
+        super(strCopy); //calling the constructor of the copier class
16
+    }
17
+
18
+    public void run(){
19
+        while (this.stringIterator.hasNext()){ //while the iterator/monkey/thread has the next string , try locking it
20
+            lock.tryLock(); //this makes the thread safe by trying to lock it
21
+            this.copied += this.stringIterator.next() + " ";
22
+            lock.unlock();
23
+        }
24
+    }
25
+}

+ 3
- 0
src/main/java/io/zipcoder/UnsafeCopier.java View File

@@ -10,5 +10,8 @@ public class UnsafeCopier extends Copier {
10 10
     }
11 11
 
12 12
     public void run() {
13
+        while (this.stringIterator.hasNext()){ //while the iterator/monkey/thread has the next string , try locking it
14
+            this.copied += this.stringIterator.next() + " ";
15
+        }
13 16
     }
14 17
 }