Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ScheduledExecutorService timeoutScheduler = Executors.newScheduledThreadPool(1);
- ScheduledFuture<?> timeoutHandler = timeoutScheduler.schedule(() -> {
- try {
- socketConnection.writeData("String that should be sent if time is out");
- turnstileClosed.removeAllListeners();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }, 10_000, TimeUnit.MILLISECONDS);
- turnstileClosed.addListener((GpioPinListenerDigital) event -> {
- try {
- if (event.getState() == PinState.HIGH) {
- socketConnection.writeData("String that should be sent within 10 seconds");
- timeoutHandler.cancel(true);
- timeoutScheduler.shutdownNow();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- timeoutScheduler.awaitTermination(10_000, TimeUnit.MILLISECONDS);
- turnstileClosed.removeAllListeners();
Add Comment
Please, Sign In to add comment