Abuse Posted November 10, 2016 Share Posted November 10, 2016 (edited) Hey, I was wondering if it's possible to interrupt the main loop from another thread? Let's say I do a sleep(10000); in the onLoop() Is there a possibility to interrupt it? Edited November 10, 2016 by Abuse Link to comment Share on other sites More sharing options...
Token Posted November 10, 2016 Share Posted November 10, 2016 (edited) @@Override public void onStart() { ExecutorService executor = Executors.newCachedThreadPool(); executor.submit(() -> { while (bot.getScriptExecutor().isRunning()) { if (... condition to interrupt ...) { bot.getEventExecutor().interrupt(); } try { Thread.sleep(100); } catch (InterruptedException e) { log(Throwables.getStackTraceAsString(e)); } } }); } @@Override public int onLoop() { log("Executing loop"); execute(new Event() { @@Override public int execute() { try { sleep(10000); } catch (InterruptedException e) { setFailed(); } setFinished(); return -1; } }); return statement; } Edited November 10, 2016 by Token 3 Link to comment Share on other sites More sharing options...
Abuse Posted November 10, 2016 Author Share Posted November 10, 2016 @[member='Override'] public void onStart() { ExecutorService executor = Executors.newCachedThreadPool(); executor.submit(() -> { while (bot.getScriptExecutor().isRunning()) { if (... condition to interrupt ...) { bot.getEventExecutor().interrupt(); } try { Thread.sleep(100); } catch (InterruptedException e) { log(Throwables.getStackTraceAsString(e)); } } }); } @[member='Override'] public int onLoop() { log("Executing loop"); execute(new Event() { @[member='Override'] public int execute() { try { sleep(10000); } catch (InterruptedException e) { setFailed(); } setFinished(); return -1; } }); return statement; } Perfect, exactly what I was looking for! Thank you Link to comment Share on other sites More sharing options...
Saiyan Posted November 11, 2016 Share Posted November 11, 2016 Locking thread Link to comment Share on other sites More sharing options...