Parcourir la source

Done, gonna try an array on this later

Kthomas il y a 6 ans
Parent
révision
ed87c4eda1

+ 2
- 2
src/main/java/io/zipcoder/Copier.java Voir le fichier

@@ -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
 }

+ 29
- 5
src/main/java/io/zipcoder/MonkeyTypewriter.java Voir le fichier

@@ -34,18 +34,42 @@ public class MonkeyTypewriter {
34 34
         // Do all of the Monkey / Thread building here
35 35
         // For each Copier(one safe and one unsafe), create and start 5 monkeys copying the introduction to
36 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);
37 40
 
38
-
39
-
40
-
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();
41 64
         // This wait is here because main is still a thread and we want the main method to print the finished copies
42 65
         // after enough time has passed.
43 66
         try {
44
-            Thread.sleep(1000);
67
+            Thread.sleep(1000); //wait time between each thread request
45 68
         } catch(InterruptedException e) {
46 69
             System.out.println("MAIN INTERRUPTED");
47 70
         }
48
-
49 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
50 74
     }
51 75
 }

+ 3
- 3
src/main/java/io/zipcoder/SafeCopier.java Voir le fichier

@@ -7,18 +7,18 @@ import java.util.concurrent.locks.ReentrantLock;
7 7
  * Make this extend the Copier like `UnsafeCopier`, except use locks to make sure that the actual intro gets printed
8 8
  * correctly every time.  Make the run method thread safe.
9 9
  */
10
-public class SafeCopier extends Copier{
10
+public class SafeCopier extends Copier{ //this will inherit the behavior of the abstract copier class
11 11
 
12 12
     private Lock lock = new ReentrantLock();
13 13
 
14 14
     public SafeCopier(String strCopy){
15
-        super(strCopy);
15
+        super(strCopy); //calling the constructor of the copier class
16 16
     }
17 17
 
18 18
     public void run(){
19 19
         while (this.stringIterator.hasNext()){ //while the iterator/monkey/thread has the next string , try locking it
20 20
             lock.tryLock(); //this makes the thread safe by trying to lock it
21
-            this.copied += this.stringIterator.next() + "";
21
+            this.copied += this.stringIterator.next() + " ";
22 22
             lock.unlock();
23 23
         }
24 24
     }

+ 1
- 1
src/main/java/io/zipcoder/UnsafeCopier.java Voir le fichier

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