Nbacon Posted June 17, 2020 Share Posted June 17, 2020 (edited) So I wrote this code to make a webwalk that could be used for Webwalk and drink stamina potion, turn on prayers , alch, eating. Im wondering if there is a better way to doing threading becuase I hate having a while loop with a thread sleep . My code. (x, y, z) -> { PathPreferenceProfile profile = PathPreferenceProfile.DEFAULT; int run = 20; boolean cameraMove = true; Procedure threadTask = null; WebWalkEvent walkEvent = null; boolean done = false; //This can take args in any order while (first(x) != null || !(first(x) instanceof Area) || !(first(x) instanceof Area)) { if (first(x) instanceof Procedure) { threadTask = (Procedure) first(x); x = rest(x); } if (first(x) instanceof PathPreferenceProfile) { profile = (PathPreferenceProfile) first(x); x = rest(x); } if (first(x) instanceof Long || first(x) instanceof Integer) { run = Math.toIntExact(num(first(x))); x = rest(x); } if (first(x) instanceof Boolean) { cameraMove = truth(first(x)); x = rest(x); } } if (first(x) instanceof Area) { walkEvent = new WebWalkEvent(listToAreaArry(x)); } if (first(x) instanceof Position) { walkEvent = new WebWalkEvent(listToPositionArry(x)); } if (walkEvent != null) { ExecutorService executor = Executors.newSingleThreadExecutor(); //this is the code //That I would like input on. if (threadTask != null) { final Procedure finalthreadTask = threadTask; Runnable task = () -> { while (true) { try { scemeBot.getScheme().eval(finalthreadTask); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }; executor.submit(task); } walkEvent.setPathPreferenceProfile(profile); walkEvent.setEnergyThreshold(run); walkEvent.setMoveCameraDuringWalking(cameraMove); done = scemeBot.walking.execute(walkEvent).hasFinished(); if (threadTask != null) { executor.shutdownNow(); } return done; } return null; } Edited June 17, 2020 by Nbacon 1 Quote Link to comment Share on other sites More sharing options...
Camaro Posted June 17, 2020 Share Posted June 17, 2020 Execute an async event instead Quote Link to comment Share on other sites More sharing options...
Nbacon Posted June 18, 2020 Author Share Posted June 18, 2020 44 minutes ago, Camaro said: Execute an async event instead So I looked on git hub for async events and there are not alot of them. But I can up with these I have no idea how it works. if (walkEvent != null) { walkEvent.setPathPreferenceProfile(profile); walkEvent.setEnergyThreshold(run); walkEvent.setMoveCameraDuringWalking(cameraMove); Event event =null; if (threadTask != null) { final Procedure finalThreadTask = threadTask; event = new Event() { @Override public int execute() throws InterruptedException { scemeBot.getScheme().eval(finalThreadTask); return 1000; } }; event.setAsync(); scemeBot.execute(event); } boolean done = scemeBot.execute(walkEvent).hasFinished(); if (event !=null){ event.setFinished(); } return done; } or if (walkEvent != null) { walkEvent.setPathPreferenceProfile(profile); walkEvent.setEnergyThreshold(run); walkEvent.setMoveCameraDuringWalking(cameraMove); Event event =null; if (threadTask != null) { final Procedure finalThreadTask = threadTask; event = new Event() { @Override public int execute() throws InterruptedException { scemeBot.getScheme().eval(finalThreadTask); return 1000; } }; event.setAsync(); walkEvent.addChild(event); } boolean done = scemeBot.execute(walkEvent).hasFinished(); return done; } Quote Link to comment Share on other sites More sharing options...
Camaro Posted June 18, 2020 Share Posted June 18, 2020 First one should work fine. I've never used the add/remove children methods, so not too sure about the second one, but looks like it should work. 1 Quote Link to comment Share on other sites More sharing options...
Nbacon Posted June 18, 2020 Author Share Posted June 18, 2020 Thanks man for your time. Last problem, priority? In this video you can see him run and eat but he goes for the food no eating then goes back to running. Quote Link to comment Share on other sites More sharing options...
Camaro Posted June 18, 2020 Share Posted June 18, 2020 2 hours ago, Nbacon said: Thanks man for your time. Last problem, priority? In this video you can see him run and eat but he goes for the food no eating then goes back to running. Well, that just comes down to the fact that you have multiple threads trying to take control of the mouse at the same time. For this situation, its probably better to just break from the webwalk event to eat. Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted June 18, 2020 Share Posted June 18, 2020 Or just break the webwalk event when you need to eat continue after ... Quote Link to comment Share on other sites More sharing options...
Nbacon Posted June 18, 2020 Author Share Posted June 18, 2020 (edited) 10 hours ago, Khaleesi said: Or just break the webwalk event when you need to eat continue after ... This is the hack i came up with is there something better? this.setBlocking(); scemeBot.getScheme().eval(finalThreadTask); this.setAsync(); Also tryed (does not work) synchronized (webwalk) { webwalk.wait(); } scemeBot.getScheme().eval(finalThreadTask); webwalk.notify(); Edited June 18, 2020 by Nbacon Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted June 18, 2020 Share Posted June 18, 2020 (edited) 3 hours ago, Nbacon said: This is the hack i came up with is there something better? this.setBlocking(); scemeBot.getScheme().eval(finalThreadTask); this.setAsync(); Also tryed (does not work) synchronized (webwalk) { webwalk.wait(); } scemeBot.getScheme().eval(finalThreadTask); webwalk.notify(); I just stop the whole webwalkerevent with a breakcondition when i have to eat and then eat myself... Then just start a new webwalk event... ^^ There 50 ways of doing the same thing, whatever works for you is good Edited June 18, 2020 by Khaleesi 1 Quote Link to comment Share on other sites More sharing options...