简体   繁体   English

Thread.sleep() / robots.delay() 更准确?

[英]Thread.sleep() / robot.delay() more accurate?

I want to write a bot for a online game using the Robot class.我想使用 Robot 类为在线游戏编写机器人。 My problem is now, that the method Thread.sleep() or robot.delay() is to inaccurate.我现在的问题是,方法 Thread.sleep() 或 robots.delay() 是不准确的。 Outside the game they work perfectly fine, with a deviation of approximately only 2 - 3 ms.在游戏之外,它们工作得非常好,偏差大约只有 2 - 3 毫秒。 But when the game is in focus, the methods have a deviation of +5 - +20 ms or even more.但是当游戏聚焦时,这些方法会有+5 - +20 ms 甚至更多的偏差。 That is sadly enaugh to make my bot unusable.可悲的是,这足以使我的机器人无法使用。 Is there any way to make these methods more accurate?有没有办法让这些方法更准确? Or are there any other ways to solve this problem?或者有没有其他方法可以解决这个问题?

There is no difference没有区别

If you browse the source for the JDK, Robot.delay() ends up calling Thread.sleep() .如果您浏览JDK 的源代码Robot.delay()最终会调用Thread.sleep()

public void delay(int ms) {
    checkDelayArgument(ms);
    Thread thread = Thread.currentThread();
    if (!thread.isInterrupted()) {
        try {
            Thread.sleep(ms);
        } catch (final InterruptedException ignored) {
            thread.interrupt(); // Preserve interrupt status
        }
    }
}

You might be able to give the Java process a higher priority then the game, tasks might be executed more quickly after being given to the scheduler.您可能可以为 Java 进程提供比游戏更高的优先级,任务在分配给调度程序后可能会更快地执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM