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.

Wait, Sleep and Threads explained please?

Featured Replies

I'm an experienced .NET developer with some Java knowledge and I'm new to the OSBot library.

 

I've been having trouble to get my script to sleep before the end of a loop.

 

Cause scenario: wait until a widget is visible. In this case I'd just wait 500 ms but it seems like none of my efforts work..

 

Whether I use wait(500) or sleep(500) the execution just goes to the next line immediately. I have a hunch this is to do with the fact that sleep methods only stop the thread from which they were called. And java applications can spin up as many threads as they want so it seems the entire concept of wait and sleep is very experimental. Is it bad practise to use these methods? Should you try to only sleep when you return from the loop function? 

2 hours ago, crazywizard94 said:

I'm an experienced .NET developer with some Java knowledge and I'm new to the OSBot library.

 

I've been having trouble to get my script to sleep before the end of a loop.

 

Cause scenario: wait until a widget is visible. In this case I'd just wait 500 ms but it seems like none of my efforts work..

 

Whether I use wait(500) or sleep(500) the execution just goes to the next line immediately. I have a hunch this is to do with the fact that sleep methods only stop the thread from which they were called. And java applications can spin up as many threads as they want so it seems the entire concept of wait and sleep is very experimental. Is it bad practise to use these methods? Should you try to only sleep when you return from the loop function? 

Go take the scripting crash course bro
https://osbot.org/forum/topic/115124-explvs-scripting-101/

easy :doge:

 

public static void main(String[] args) {
    conditionalSleep(() -> false, 5_000);
}

public static void conditionalSleep(BooleanSupplier condition, long sleepTime) {
    final long startTime = System.currentTimeMillis();
    do {
        if (System.currentTimeMillis() >= startTime + sleepTime) {
            break;
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {

        }
    } while (!condition.getAsBoolean());
}
7 hours ago, crazywizard94 said:

I'm an experienced .NET developer with some Java knowledge and I'm new to the OSBot library.

 

I've been having trouble to get my script to sleep before the end of a loop.

 

Cause scenario: wait until a widget is visible. In this case I'd just wait 500 ms but it seems like none of my efforts work..

 

Whether I use wait(500) or sleep(500) the execution just goes to the next line immediately. I have a hunch this is to do with the fact that sleep methods only stop the thread from which they were called. And java applications can spin up as many threads as they want so it seems the entire concept of wait and sleep is very experimental. Is it bad practise to use these methods? Should you try to only sleep when you return from the loop function? 

for a static sleep you can just use sleep, not really recommended since a bit of lag can mess up your script pretty quick.
And you don't wanna sleep fro to long, bcs that will make the script really slow^^

You can use conditional sleeps instead :)
https://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html

For example:

new ConditionalSleep( 5000, 100) {
	@Override
	public boolean condition() throws InterruptedException {
		return script.myPlayer().isAnimating();
	}
}.sleep();

Sleeps for a maximum of 5000ms OR until the player is animating.
This will check every 100ms

  • Author
8 hours ago, Malcolm said:

easy :doge:

 

public static void main(String[] args) {
    conditionalSleep(() -> false, 5_000);
}

public static void conditionalSleep(BooleanSupplier condition, long sleepTime) {
    final long startTime = System.currentTimeMillis();
    do {
        if (System.currentTimeMillis() >= startTime + sleepTime) {
            break;
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {

        }
    } while (!condition.getAsBoolean());
}

Okay so with the conditionalSleep() you just supply the condition? I'm gonna assume this is thread-safe. Cheers mate

13 hours ago, ExtraBotz said:

 

Lol I've seen that series but cocky me decided to start on Episode 3 😁 

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.