Jump to content

How to Multi-thread [Easy]


dreameo

Recommended Posts

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
  • Like 3
  • Mald 1
Link to comment
Share on other sites

  • 5 months later...
  • 2 weeks later...
  • 11 months later...
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);
  • Heart 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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