Pārlūkot izejas kodu

Excellent beginner excercise, done

Kaitrina High 6 gadus atpakaļ
vecāks
revīzija
2af55e7ab0

+ 7
- 0
src/main/java/io/zipcoder/Copier.java Parādīt failu

@@ -19,3 +19,10 @@ public abstract class Copier implements Runnable {
19 19
 
20 20
     public abstract void run();
21 21
 }
22
+
23
+/**Runnable is a interface,
24
+ * to implement runnable there is a method called run that gets implemented in the background
25
+ * and the copier class is abstract but it has a run method stubbed out which all of the
26
+ * subclasses have to implement. Unsafe and safe copier both have their own run method which are
27
+ * both slightly different.
28
+ */

+ 40
- 1
src/main/java/io/zipcoder/MonkeyTypewriter.java Parādīt failu

@@ -23,7 +23,30 @@ 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
+        UnsafeCopier badMonkey = new UnsafeCopier(introduction);
27
+       SafeCopier goodMonkey = new SafeCopier(introduction);
26 28
 
29
+        Thread unsafeWorker1 = new Thread(badMonkey);
30
+        Thread unsafeWorker2 = new Thread(badMonkey);
31
+        Thread unsafeWorker3 = new Thread(badMonkey);
32
+        Thread unsafeWorker4 = new Thread(badMonkey);
33
+        Thread unsafeWorker5 = new Thread(badMonkey);
34
+        Thread safeWorker1 = new Thread(goodMonkey);
35
+        Thread safeWorker2 = new Thread(goodMonkey);
36
+        Thread safeWorker3 = new Thread(goodMonkey);
37
+        Thread safeWorker4 = new Thread(goodMonkey);
38
+        Thread safeWorker5 = new Thread(goodMonkey);
39
+
40
+        unsafeWorker1.start();
41
+        unsafeWorker2.start();
42
+        unsafeWorker3.start();
43
+        unsafeWorker4.start();
44
+        unsafeWorker5.start();
45
+        safeWorker1.start();
46
+        safeWorker2.start();
47
+        safeWorker3.start();
48
+        safeWorker4.start();
49
+        safeWorker5.start();
27 50
 
28 51
         // This wait is here because main is still a thread and we want the main method to print the finished copies
29 52
         // after enough time has passed.
@@ -34,5 +57,21 @@ public class MonkeyTypewriter {
34 57
         }
35 58
 
36 59
         // Print out the copied versions here.
60
+        System.out.println(badMonkey.copied);
61
+        System.out.println("------------------------------------------------");
62
+        System.out.println(goodMonkey.copied);
37 63
     }
38
-}
64
+}
65
+
66
+
67
+/**
68
+ * created 1 instance of unsafe copier and I pushed it to 5 different threads. Called start on each thread
69
+ * what that does is everytime you tell a thread to start it dispatches to the background. Up until now
70
+ * everything running sequentially on main. Now think of main thread  trunk of a tree and it branches
71
+ * 5 times. What that means is that all those workers instead of running sequentially are going to run all at the same
72
+ * time. In unsafe copier method this instance is called once...there is only one copy of this instance and it's
73
+ * being called by 5 threads at the same time. Side effect of that is depending on the order in which the threads execute
74
+ * you will never have any control over the output. Inside unsafe copier there's a race condition---more than one process
75
+ * has access to the resource at the same time.
76
+
77
+ */

+ 49
- 2
src/main/java/io/zipcoder/SafeCopier.java Parādīt failu

@@ -1,8 +1,55 @@
1 1
 package io.zipcoder;
2
-
2
+import java.util.NoSuchElementException;
3
+import java.util.concurrent.locks.Lock;
4
+import java.util.concurrent.locks.ReadWriteLock;
5
+import java.util.concurrent.locks.ReentrantReadWriteLock;
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
+    // stole this from here: http://www.baeldung.com/java-concurrent-locks
12
+    ReadWriteLock lock = new ReentrantReadWriteLock();
13
+    Lock writeLock = lock.writeLock();
14
+
15
+    public SafeCopier(String toCopy) {
16
+        super(toCopy);
17
+    }
18
+
19
+    public void run() {
20
+        try {
21
+            while (this.stringIterator.hasNext()) {
22
+                writeLock.lock();
23
+                this.copied += stringIterator.next() + " ";// + Thread.currentThread().getName();
24
+                writeLock.unlock();
25
+                Thread.sleep(10L);
26
+            }
27
+        }
28
+        catch (NoSuchElementException ex) {
29
+            this.copied += "FAIL ";
30
+        } catch (InterruptedException e) {
31
+            e.printStackTrace();
32
+        }
33
+    }
8 34
 }
35
+
36
+
37
+/**there's a variety of locks
38
+ * readwrite lock -- once you have the lock no else can read or write to it
39
+ * read lock -- uncommon if you have lock no can read it but can write to it
40
+ * write lock -most common if you have the lock you can't write to it but you can read it
41
+ * Want to keep locks locked as little as possible so program doesn't bog down. So put
42
+ * writeLock near the thing I want to protect. Once create lock the next thread that comes through and tries to
43
+ * grab the lock it can't so that thread stops and its halted. Once it unlocked all the threads fight for the lock.
44
+ * fight===> a whole bunch of memory shit I don't understand is working under the hood to control the thread scheduling.
45
+ *Also its up to the operating system, it s the operating system that gives you threads. This program could function
46
+ * differently on different computers.
47
+ * Once the thread is unlocked it goes to sleep for 10 milliseconds...the reason for that is that the same thread
48
+ * doesn't take the lock multiple times in row. Sort of like the guy who butts in line repeatedly at the buffet.
49
+ * YOu have to catch the interupted exception if you put a thread to sleep...the ide forces you to do it
50
+ * Had to add the interrupted exception in case.
51
+ *
52
+ * Replace this above the unlock  to get visibility into what threads are accessing the program
53
+ *  this.copied += Thread.currentThread().getName() + " ";
54
+ */
55
+

+ 18
- 1
src/main/java/io/zipcoder/UnsafeCopier.java Parādīt failu

@@ -1,14 +1,31 @@
1 1
 package io.zipcoder;
2
-
2
+import java.util.NoSuchElementException;
3 3
 /**
4 4
  * Modify the run function so that the monkeys each grab the next word and write it to the copy.
5 5
  */
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
+        try {
15
+            while (this.stringIterator.hasNext()) {
16
+                this.copied += this.stringIterator.next() + " ";
17
+            }
18
+        }
19
+        catch (NoSuchElementException ex) {
20
+            this.copied += "FAIL ";
21
+        }
13 22
     }
14 23
 }
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+