|
@@ -1,8 +1,37 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.concurrent.locks.ReadWriteLock;
|
|
4
|
+import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
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 {
|
|
11
|
+
|
|
12
|
+ ReadWriteLock instanceOfReadWriteLock;
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+ public SafeCopier(String toCopy) {
|
|
16
|
+ super(toCopy);
|
|
17
|
+ instanceOfReadWriteLock = new ReentrantReadWriteLock();
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ public void run() {
|
|
21
|
+
|
|
22
|
+ instanceOfReadWriteLock.writeLock().lock();
|
|
23
|
+ instanceOfReadWriteLock.readLock().lock();
|
|
24
|
+
|
|
25
|
+ try {
|
|
26
|
+ while (stringIterator.hasNext()) {
|
|
27
|
+ copied += stringIterator.next() + " ";
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ } finally {
|
|
31
|
+ instanceOfReadWriteLock.writeLock().unlock();
|
|
32
|
+ instanceOfReadWriteLock.readLock().unlock();
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+}
|