All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mapsmessaging.devices.gpio.ThreadInterruptExecutor Maven / Gradle / Ivy

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);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy