dreameo Posted June 29, 2018 Share Posted June 29, 2018 (edited) 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: Spoiler https://imgur.com/a/lZl1wId 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 January 15, 2019 by dreameo 3 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted June 29, 2018 Share Posted June 29, 2018 (edited) 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 June 29, 2018 by Explv 3 2 Quote Link to comment Share on other sites More sharing options...
lebotman Posted July 4, 2018 Share Posted July 4, 2018 Nice tutorial, very useful. Quote Link to comment Share on other sites More sharing options...
Camaro Posted January 1, 2019 Share Posted January 1, 2019 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? Quote Link to comment Share on other sites More sharing options...
dreameo Posted January 15, 2019 Author Share Posted January 15, 2019 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. Quote Link to comment Share on other sites More sharing options...
dobin2 Posted January 6, 2020 Share Posted January 6, 2020 (edited) 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 January 7, 2020 by dobin2 Quote Link to comment Share on other sites More sharing options...
BravoTaco Posted January 7, 2020 Share Posted January 7, 2020 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); 1 Quote Link to comment Share on other sites More sharing options...
ProjectPact Posted January 7, 2020 Share Posted January 7, 2020 If you are doing threads, stop() is depreciated. You'll need to interrupt them accordingly. Quote Link to comment Share on other sites More sharing options...
dreameo Posted January 10, 2020 Author Share Posted January 10, 2020 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. Quote Link to comment Share on other sites More sharing options...