Jump to content

Practical uses for MultiThreading


Recommended Posts

Posted

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 :D 

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;
    }
}

 

Posted
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?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...