英文:
Thread.sleep() / robot.delay() more accurate?
问题
我想使用Robot类为在线游戏编写一个机器人。我的问题是,方法Thread.sleep()或robot.delay()的精度太低。在游戏之外,它们表现得非常好,偏差仅约为2 - 3毫秒。但是当游戏处于焦点状态时,这些方法的偏差为+5 - +20毫秒甚至更多。这使得我的机器人无法使用。是否有任何方法可以使这些方法更精确?或者是否有其他方法来解决这个问题?
英文:
I want to write a bot for a online game using the Robot class. My problem is now, that the method Thread.sleep() or robot.delay() is to inaccurate. Outside the game they work perfectly fine, with a deviation of approximately only 2 - 3 ms. But when the game is in focus, the methods have a deviation of +5 - +20 ms or even more. 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?
答案1
得分: 1
没有任何区别
如果你浏览源代码查看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(); // 保留中断状态
}
}
}
你也许可以给Java进程赋予更高的优先级,然后游戏在交给调度程序后可能会更快地执行任务。
英文:
There is no difference
If you browse the source for the JDK, Robot.delay()
ends up calling 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论