Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to Multi-thread [Easy]

Featured Replies

Here is how you can multi thread. Why would you want to? Sometimes you want to be doing x and y at the same time. In a single thread, you can only do one thing at a time.

Note: There are different ways to multi thread and this isn't some kind of perfect way to do it.

In this example there are two classes:

  • Main Thread
  • Demo thread (where we run our second thread)

 

Demo Thread

The most important thing for the demo thread is that you implement Runnable.  The second most important thing is the boolean, 'run'. We need to tell the thread when to stop running because in this case, we are in a loop. Had there been no loop and we just began the thread, it would be like any other execution, it runs once and then ends. In that case, we wouldn't need to worry about storing a boolean.

So here is the Demo Thread Example:

edit: added volatile keyword to boolean - important!

Spoiler

import org.osbot.rs07.script.MethodProvider;

public class ThreadDemo extends MethodProvider implements Runnable {

    private volatile boolean run = true;

    @Override
    public void run() {
        while(run)
        {
            try {
                log("This is a separate thread running (Demo Thread)");
                Thread.sleep(256);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop(){
        run = false;
    }
}

Main Thread

We need to do one extra thing before we can start with our thread. Since I extended method provider, I need to exchange context (First line after the onStart method).

Now the actual multithreading part you've been waiting for: haha, just create a new thread and run your instance of runnable . Yeah that's it..

Only thing we have to do is make sure we stop our new thread once we stop our script. If we don't, the additional thread we made will keep running even if we stop the bot. If you start and stop the bot several times, you'll have a number of threads (demo threads that we made) running. We created a flag that tells the thread when to 'stop' executing. In the onExit method, just use the stop method we build which sets the flag to false (stop running).

Spoiler

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(name = "Thread Demo", version = .01, author = "Dream", logo = "", info = "")
public class Main extends Script {

    private ThreadDemo demo = new ThreadDemo();

    @Override
    public void onStart() throws InterruptedException {
        demo.exchangeContext(getBot());
        new Thread(demo).start();
    }

    @Override
    public int onLoop() throws InterruptedException {
        log("This is the main thread running");
        return 256;
    }

    @Override
    public void onExit() throws InterruptedException {
        demo.stop();
    }
}

Here's just a snippet of the output from the log:

You don't have to multi thread and a lot of times, you can probably find solutions in a single thread instead of multi threading.

Good luck

 

Edited by dreameo

An alternative would be to use OSBot's Event with the async option set:
 

Event customEvent = new Event() {
    @Override
    public int execute() throws InterruptedException {
        return 0;
    }
};
customEvent.setAsync();
execute(customEvent);

 

Edited by Explv

  • 5 months later...
On 6/29/2018 at 10:29 AM, Explv said:

An alternative would be to use OSBot's Event with the async option set:

Are there any advantages to using this option instead of multithreading?

  • 2 weeks later...
  • Author
On 1/1/2019 at 4:38 PM, camaro 09 said:

Are there any advantages to using this option instead of multithreading?

They are both multi-threading. Explv response is osbot specific and defeats the purpose of the tutorial.

  • 11 months later...

Could anyone please show me how to create and run an ASync event? Sorry for the noob question, I am very new to all this scripting stuff.

Edited by dobin2

17 hours ago, dobin2 said:

Could anyone please show me how to create and run an ASync event? Sorry for the noob question, I am very new to all this scripting stuff.

If you are wanting to use the Event class in the OSBot API than its rather easy to do.

First declare an Event object. attackTarget(), would be a custom method that handles finding an NPC to attack, and than also attacking that target.

private Event attackEvent = new Event() {
    @Override
    public int execute() throws InterruptedException {
        if (!myPlayer().isUnderAttack()) {
            attackTarget();
        }
        return 0;
    }
};

Than in you're onStart you can set the attackEvent to be an Async operation.

attackEvent.setAsync();

When you want to execute the event you than call the execute() method and pass the Event object name into it.

execute(attackEvent);

If you are doing threads, stop() is depreciated. You'll need to interrupt them accordingly. 

  • Author
On 1/7/2020 at 1:42 PM, ProjectPact said:

If you are doing threads, stop() is depreciated. You'll need to interrupt them accordingly. 

I'm not using the Thread's API stop(). I'm using my own stop() method.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.