|
@@ -9,18 +9,18 @@ import java.util.concurrent.locks.ReentrantLock;
|
9
|
9
|
*/
|
10
|
10
|
public class SafeCopier extends Copier {
|
11
|
11
|
|
12
|
|
- Lock sharedLock = new ReentrantLock();
|
13
|
|
-
|
14
|
12
|
public SafeCopier(String toCopy) {
|
15
|
13
|
super(toCopy);
|
16
|
14
|
}
|
17
|
15
|
|
18
|
16
|
public void run() {
|
19
|
|
- sharedLock.lock();
|
20
|
|
- if (stringIterator.hasNext()) {
|
21
|
|
- copied += stringIterator.next() + " ";
|
|
17
|
+ while (stringIterator.hasNext()) {
|
|
18
|
+ synchronized (stringIterator) {
|
|
19
|
+ if (stringIterator.hasNext()) {
|
|
20
|
+ copied += stringIterator.next() + " ";
|
|
21
|
+ }
|
|
22
|
+ }
|
22
|
23
|
}
|
23
|
|
- sharedLock.unlock();
|
24
|
24
|
}
|
25
|
25
|
|
26
|
26
|
}
|