Author: krosenvold
Date: Fri Jun 19 18:20:42 2015
New Revision: 1686477
URL: http://svn.apache.org/r1686477
Log:
IO-488 FileUtils.waitFor(...) swallows thread interrupted status
Patch by Björn Buchner, testcase by me
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsWaitForTestCase.java
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1686477&r1=1686476&r2=1686477&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
(original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
Fri Jun 19 18:20:42 2015
@@ -1706,19 +1706,26 @@ public class FileUtils {
public static boolean waitFor(final File file, final int seconds) {
int timeout = 0;
int tick = 0;
- while (!file.exists()) {
- if (tick++ >= 10) {
- tick = 0;
- if (timeout++ > seconds) {
- return false;
+ boolean wasInterrupted = false;
+ try {
+ while (!file.exists()) {
+ if (tick++ >= 10) {
+ tick = 0;
+ if (timeout++ > seconds) {
+ return false;
+ }
+ }
+ try {
+ Thread.sleep(100);
+ } catch (final InterruptedException ignore) {
+ wasInterrupted = true;
+ } catch (final Exception ex) {
+ break;
}
}
- try {
- Thread.sleep(100);
- } catch (final InterruptedException ignore) {
- // ignore exception
- } catch (final Exception ex) {
- break;
+ } finally {
+ if (wasInterrupted) {
+ Thread.currentThread().interrupt();
}
}
return true;
Modified:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsWaitForTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsWaitForTestCase.java?rev=1686477&r1=1686476&r2=1686477&view=diff
==============================================================================
---
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsWaitForTestCase.java
(original)
+++
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsWaitForTestCase.java
Fri Jun 19 18:20:42 2015
@@ -17,6 +17,8 @@
package org.apache.commons.io;
import java.io.File;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.io.testtools.FileBasedTestCase;
@@ -52,4 +54,23 @@ public class FileUtilsWaitForTestCase ex
FileUtils.waitFor(new File(""), 2);
}
+ public void testWaitForInterrupted() throws InterruptedException {
+ final AtomicBoolean wasInterrupted = new AtomicBoolean(false);
+ final CountDownLatch started = new CountDownLatch(1);
+ Runnable thread = new Runnable() {
+ @Override
+ public void run() {
+ started.countDown();
+ FileUtils.waitFor(new File(""), 2);
+ wasInterrupted.set( Thread.currentThread().isInterrupted());
+ }
+ };
+ Thread thread1 = new Thread(thread);
+ thread1.start();
+ started.await();
+ thread1.interrupt();
+ thread1.join();
+ assertTrue( wasInterrupted.get() );
+ }
+
}