io.mapsmessaging.devices.gpio.ThreadInterruptExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deviceLibrary Show documentation
Show all versions of deviceLibrary Show documentation
Provides a plugable Device integration and access
The newest version!
package io.mapsmessaging.devices.gpio;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
public class ThreadInterruptExecutor implements InterruptExecutor, Runnable {
private final AtomicBoolean flag;
private final InterruptHandler handler;
public ThreadInterruptExecutor(InterruptHandler handler){
flag = new AtomicBoolean(true);
this.handler = handler;
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
}
@Override
public void run() {
while(flag.get()){
try {
Thread.sleep(2);
handler.interruptFired();
} catch (IOException e) {
flag.set(false);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
flag.set(false);
}
}
}
@Override
public void close() throws IOException {
flag.set(false);
}
}