Write one string to a socket within 10 seconds or another string if the time is out
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have implemented a method which will send one data to the server if it (Raspberry Pi Pin Input HIGH, in this case) occurs within 10 seconds. If this doesn't happen within 10 seconds then I should send some other data.
This implementation works for me, but I think that there should be another better implementation.
ScheduledExecutorService timeoutScheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> timeoutHandler = timeoutScheduler.schedule(() ->
try
socketConnection.writeData("String that should be sent if time is out");
turnstileClosed.removeAllListeners();
timeoutScheduler.shutdownNow();
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();
java multithreading socket scheduled-tasks timeout
add a comment |Â
up vote
2
down vote
favorite
I have implemented a method which will send one data to the server if it (Raspberry Pi Pin Input HIGH, in this case) occurs within 10 seconds. If this doesn't happen within 10 seconds then I should send some other data.
This implementation works for me, but I think that there should be another better implementation.
ScheduledExecutorService timeoutScheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> timeoutHandler = timeoutScheduler.schedule(() ->
try
socketConnection.writeData("String that should be sent if time is out");
turnstileClosed.removeAllListeners();
timeoutScheduler.shutdownNow();
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();
java multithreading socket scheduled-tasks timeout
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have implemented a method which will send one data to the server if it (Raspberry Pi Pin Input HIGH, in this case) occurs within 10 seconds. If this doesn't happen within 10 seconds then I should send some other data.
This implementation works for me, but I think that there should be another better implementation.
ScheduledExecutorService timeoutScheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> timeoutHandler = timeoutScheduler.schedule(() ->
try
socketConnection.writeData("String that should be sent if time is out");
turnstileClosed.removeAllListeners();
timeoutScheduler.shutdownNow();
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();
java multithreading socket scheduled-tasks timeout
I have implemented a method which will send one data to the server if it (Raspberry Pi Pin Input HIGH, in this case) occurs within 10 seconds. If this doesn't happen within 10 seconds then I should send some other data.
This implementation works for me, but I think that there should be another better implementation.
ScheduledExecutorService timeoutScheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> timeoutHandler = timeoutScheduler.schedule(() ->
try
socketConnection.writeData("String that should be sent if time is out");
turnstileClosed.removeAllListeners();
timeoutScheduler.shutdownNow();
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();
java multithreading socket scheduled-tasks timeout
edited Mar 22 at 15:14
asked Mar 21 at 10:26
Khojiakbar
113
113
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190116%2fwrite-one-string-to-a-socket-within-10-seconds-or-another-string-if-the-time-is%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password