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