Skip to content

FileAlternationMonitor#stop() does not stop the thread#36

Closed
anthonyraymond wants to merge 1 commit into
apache:masterfrom
anthonyraymond:master
Closed

FileAlternationMonitor#stop() does not stop the thread#36
anthonyraymond wants to merge 1 commit into
apache:masterfrom
anthonyraymond:master

Conversation

@anthonyraymond

@anthonyraymond anthonyraymond commented May 1, 2017

Copy link
Copy Markdown

The thread if FileAlterationMonitor wasn't stopped by the stop(int) method, which forbid application to shutdown unless all Thread are exited (if FileAlterationMonitor is part of a DI managed component).

This behavior conflict with the method javadoc @param stopInterval the amount of time in milliseconds to wait for the thread to finish.

Simple example to understand why i changed the code

    Thread t = new Thread(() -> {
        try {
            Thread.sleep(500000);
        } catch (final InterruptedException e) {
        }
    });
    t.start();
    t.join(50);
   // Ok, we reach this point until 500000ms are elapsed, but the thread is still alive.
   //   because Thread#join(int) does not kill the thread. And the thread remains alive.
    Thread t = new Thread(() -> {
        try {
            Thread.sleep(500000);
        } catch (final InterruptedException e) {
        }
    });
    t.start();
    t.join(50);
    t.interupt();
   // Thread is exited

In this case, we waited the given time BEFORE exiting the Thread, as described in the javadoc, and the Thread is now finished and killed.

@anthonyraymond

Copy link
Copy Markdown
Author

@coveralls

coveralls commented May 2, 2017

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 90.086% when pulling bf49922 on anthonyraymond:master into bfd83b0 on apache:master.

@anthonyraymond anthonyraymond changed the title FileAlternationMonitor#stop() not stop the thread FileAlternationMonitor#stop() does not stop the thread May 2, 2017
thread.join(stopInterval);
thread.interrupt();
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is swallowing an InterruptedException a really bad practice (at least according to Java Concurrency in Practice and https://www.ibm.com/developerworks/library/j-jtp05236/)?

@anthonyraymond anthonyraymond May 3, 2017

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tell me if i'm wrong, because it is kind of obscure.

The FileAlterationMonitor embed a thread. But when we call stop() we are calling from another thread (most likely the main thread).
Isn't Thread.currentThread().interrupt(); going to stop the calling thread? In our case the main thread?

Now, i agree that catching an exception to do nothing sounds dumb. But in our case we want to stop the inner thread. So even if it is interupted while performing the join() is kind of a good news. It just stopped earlier than expected.

Am i correct?

@garydgregory garydgregory Mar 10, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR @anthonyraymond

Regardless of the implementation of this fix, it would be great to provide a unit test that fails without the main patch. Otherwise, "confidence is low" and regression will happen.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @garydgregory .
@SvetlinZarev was faster than i was: #58

@SvetlinZarev SvetlinZarev mentioned this pull request Mar 10, 2018
asfgit pushed a commit that referenced this pull request Apr 29, 2018
Interrupt the thread created by FileAlterationMonitor on stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants