Search code examples
javamultithreadingnio

java.nio.channels.ClosedSelectorException after finishing main code


I have a main code

and after it finishes I get this error:

How can it be? it means a new background thread is opened?

How can i terminate all of them eventually?

2016-08-07 00:00:42 ERROR linqmap.nio.NIOSelector Unexpected error in select loop
java.nio.channels.ClosedSelectorException
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:83)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at linqmap.nio.NIOSelector.runSelector(NIOSelector.java:627)
    at linqmap.nio.NIOSelector$5.run(NIOSelector.java:477)
    at java.lang.Thread.run(Thread.java:745)
2016-08-07 00:00:42 ERROR linqmap.nio.NIOSelector Unexpected error in select loop
java.nio.channels.ClosedSelectorException
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:83)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at linqmap.nio.NIOSelector.runSelector(NIOSelector.java:627)
    at linqmap.nio.NIOSelector$5.run(NIOSelector.java:477)
    at java.lang.Thread.run(Thread.java:745)
2016-08-07 00:00:42 ERROR linqmap.nio.NIOSelector Unexpected error in select loop
java.nio.channels.ClosedSelectorException
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:83)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at linqmap.nio.NIOSelector.runSelector(NIOSelector.java:627)
    at linqmap.nio.NIOSelector$5.run(NIOSelector.java:477)
    at java.lang.Thread.run(Thread.java:745)

Solution

  • You closed your selector while it was inside select().

    If you're trying to stop it, you should set a boolean that is checked every time around the select loop and then call Selector.wakeup().

    Note that the select loop has to cope with being woken up when there are zero ready SelectionKeys.