diff --git a/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalInput.java b/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalInput.java index f4c2d5a6..baf13737 100644 --- a/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalInput.java +++ b/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalInput.java @@ -13,6 +13,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; /** *

PiGpioDigitalOutput class.

@@ -69,12 +70,14 @@ public DigitalInput initialize(Context context) throws InitializeException { @Override public void run() { DigitalState lastState = null; - while (true) { + while (!Thread.interrupted()) { long debounceNs = GpioDDigitalInput.this.debounceNs; // We have to use this function before calling eventRead() directly, since native methods can't be interrupted. // eventRead() is blocking and prevents thread interrupt while running while (!GpioDDigitalInput.this.line.eventWait(inputMaxWaitNs)) { - continue; + if (Thread.interrupted()) { + return; + } } GpioLineEvent lastEvent = GpioDDigitalInput.this.line.eventRead(); long currentTime = System.nanoTime(); @@ -82,6 +85,9 @@ public void run() { // Perform debouncing // If the event is too new to be sure that it is debounced then ... while (lastEvent.getTimeNs() + debounceNs >= currentTime) { + if (Thread.interrupted()) { + return; + } // ... wait for remaining debounce time and watch out for new event(s) if(GpioDDigitalInput.this.line.eventWait(Math.min(inputMaxWaitNs, lastEvent.getTimeNs() + debounceNs - currentTime))) { // Repeat if a second event occurred withing debounce interval @@ -97,7 +103,6 @@ public void run() { lastState = newState; GpioDDigitalInput.this.dispatch(new DigitalStateChangeEvent(GpioDDigitalInput.this, newState)); } - } } }; @@ -108,9 +113,14 @@ public void run() { @Override public DigitalInput shutdown(Context context) throws ShutdownException { - super.shutdown(context); - executor.shutdown(); - this.line.release(); + try { + super.shutdown(context); + executor.shutdownNow(); + executor.awaitTermination(5, TimeUnit.SECONDS); + this.line.release(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } return this; } diff --git a/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalOutput.java b/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalOutput.java index 20686c6a..f8953fc9 100644 --- a/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalOutput.java +++ b/plugins/pi4j-plugin-gpiod/src/main/java/com/pi4j/plugin/gpiod/provider/gpio/digital/GpioDDigitalOutput.java @@ -81,9 +81,9 @@ public DigitalOutput initialize(Context context) throws InitializeException { @Override public DigitalOutput shutdown(Context context) throws ShutdownException { - DigitalOutput returnMe = super.shutdown(context); + super.shutdown(context); this.line.release(); - return returnMe; + return this; } /**