Guest User

Untitled

a guest
Mar 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. ScheduledExecutorService timeoutScheduler = Executors.newScheduledThreadPool(1);
  2.  
  3. ScheduledFuture<?> timeoutHandler = timeoutScheduler.schedule(() -> {
  4. try {
  5. socketConnection.writeData("String that should be sent if time is out");
  6. turnstileClosed.removeAllListeners();
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }, 10_000, TimeUnit.MILLISECONDS);
  11.  
  12. turnstileClosed.addListener((GpioPinListenerDigital) event -> {
  13. try {
  14. if (event.getState() == PinState.HIGH) {
  15. socketConnection.writeData("String that should be sent within 10 seconds");
  16. timeoutHandler.cancel(true);
  17. timeoutScheduler.shutdownNow();
  18. }
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. });
  23.  
  24. timeoutScheduler.awaitTermination(10_000, TimeUnit.MILLISECONDS);
  25. turnstileClosed.removeAllListeners();
Add Comment
Please, Sign In to add comment