|
@@ -1,5 +1,6 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.util.concurrent.TimeUnit;
|
3
|
4
|
import java.util.concurrent.locks.Lock;
|
4
|
5
|
import java.util.concurrent.locks.ReentrantLock;
|
5
|
6
|
|
|
@@ -14,13 +15,22 @@ public class SafeCopier extends Copier {
|
14
|
15
|
}
|
15
|
16
|
|
16
|
17
|
public void run() {
|
17
|
|
- sharedLock.lock();
|
18
|
|
- while(stringIterator.hasNext()){
|
19
|
|
- sharedLock.lock();
|
20
|
|
- copied += stringIterator.next() + " ";
|
21
|
|
- sharedLock.unlock();
|
|
18
|
+ boolean keepGoing = true;
|
|
19
|
+ try {
|
|
20
|
+ TimeUnit.MILLISECONDS.sleep(30);
|
|
21
|
+ } catch (InterruptedException e){
|
|
22
|
+ e.printStackTrace();
|
22
|
23
|
}
|
23
|
|
-
|
|
24
|
+ while(keepGoing){
|
|
25
|
+ if(sharedLock.tryLock()){
|
|
26
|
+ if (stringIterator.hasNext()){
|
|
27
|
+ copied += stringIterator.next() + " " + Thread.currentThread().getName() + " ";
|
|
28
|
+ } else keepGoing = false;
|
|
29
|
+ sharedLock.unlock();
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ }
|
|
33
|
+
|
24
|
34
|
|
25
|
35
|
}
|
26
|
36
|
}
|