File tree Expand file tree Collapse file tree
main/java/org/apache/commons/io
test/java/org/apache/commons/io Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1706,19 +1706,26 @@ private static File[] verifiedListFiles(File directory) throws IOException {
17061706 public static boolean waitFor (final File file , final int seconds ) {
17071707 int timeout = 0 ;
17081708 int tick = 0 ;
1709- while (!file .exists ()) {
1710- if (tick ++ >= 10 ) {
1711- tick = 0 ;
1712- if (timeout ++ > seconds ) {
1713- return false ;
1709+ boolean wasInterrupted = false ;
1710+ try {
1711+ while (!file .exists ()) {
1712+ if (tick ++ >= 10 ) {
1713+ tick = 0 ;
1714+ if (timeout ++ > seconds ) {
1715+ return false ;
1716+ }
1717+ }
1718+ try {
1719+ Thread .sleep (100 );
1720+ } catch (final InterruptedException ignore ) {
1721+ wasInterrupted = true ;
1722+ } catch (final Exception ex ) {
1723+ break ;
17141724 }
17151725 }
1716- try {
1717- Thread .sleep (100 );
1718- } catch (final InterruptedException ignore ) {
1719- // ignore exception
1720- } catch (final Exception ex ) {
1721- break ;
1726+ } finally {
1727+ if (wasInterrupted ) {
1728+ Thread .currentThread ().interrupt ();
17221729 }
17231730 }
17241731 return true ;
Original file line number Diff line number Diff line change 1717package org .apache .commons .io ;
1818
1919import java .io .File ;
20+ import java .util .concurrent .CountDownLatch ;
21+ import java .util .concurrent .atomic .AtomicBoolean ;
2022
2123import org .apache .commons .io .testtools .FileBasedTestCase ;
2224
@@ -52,4 +54,23 @@ public void testWaitFor() {
5254 FileUtils .waitFor (new File ("" ), 2 );
5355 }
5456
57+ public void testWaitForInterrupted () throws InterruptedException {
58+ final AtomicBoolean wasInterrupted = new AtomicBoolean (false );
59+ final CountDownLatch started = new CountDownLatch (1 );
60+ Runnable thread = new Runnable () {
61+ @ Override
62+ public void run () {
63+ started .countDown ();
64+ FileUtils .waitFor (new File ("" ), 2 );
65+ wasInterrupted .set ( Thread .currentThread ().isInterrupted ());
66+ }
67+ };
68+ Thread thread1 = new Thread (thread );
69+ thread1 .start ();
70+ started .await ();
71+ thread1 .interrupt ();
72+ thread1 .join ();
73+ assertTrue ( wasInterrupted .get () );
74+ }
75+
5576}
You can’t perform that action at this time.
0 commit comments