sliskoning96 Posted February 25, 2020 Posted February 25, 2020 I am trying to understand the uses of multithreading for OSBot. My idea is that I make a thread that runs async and monitors the player's health, stats, poison,... And when a certain action is required, for example eating a piece of food, does so. I created a (very) simple script demonstrating what I would like to accomplish with the multithreading. Is this the way to accomplish such a task? PS: I am new to the OSBot API so I don't know if everything I wrote is correct import org.osbot.rs07.script.MethodProvider; public class SupplyThread extends MethodProvider implements Runnable { private volatile boolean run = true; @Override public void run() { while(run) { try { if(myPlayer().getHealthPercent() < (75 * (Math.random())) || myPlayer().getHealthPercent() < 20){ if (getInventory().contains("Tuna")) { if (getInventory().getSelectedItemName() == null) { getInventory().getItem("Tuna").interact("Eat"); } else { getInventory().deselectItem(); } } new ConditionalSleep(2000,1000) { @Override public boolean condition() throws InterruptedException { return myPlayer().getAnimation() == 829; } }.sleep(); } Thread.sleep(random(400, 650)); } catch (InterruptedException e) { e.printStackTrace(); } } } public void stop(){ run = false; } }
dreameo Posted February 25, 2020 Posted February 25, 2020 1. I know I have volatile (for boolean) added to my own guide for multithreading but I don't think it's needed. 2. This will interfere with 'main' bot since this interaction is async.
sliskoning96 Posted February 25, 2020 Author Posted February 25, 2020 1 hour ago, dreameo said: 1. I know I have volatile (for boolean) added to my own guide for multithreading but I don't think it's needed. 2. This will interfere with 'main' bot since this interaction is async. Thank you for the response @dreameo. So if I understand making a thread to take care of supplies and running that thread async is not the way to accomplish such a task? Should I put all supply logic in the main thread then?
dreameo Posted February 25, 2020 Posted February 25, 2020 I think async is fine but you need to be able to freeze all other running threads when a 'task' needs to be executed (only in some cases)