java 如何在Java中每毫秒调用一次方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5132960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:37:04  来源:igfitidea点击:

How to call a method once per ms in Java

javatimer3d

提问by Mahdi_Nine

HI. I want a function in java that automatically called.

你好。我想要一个自动调用的java函数。

for example wen we use Timeclass like blew

例如 wen 我们使用Time像 blew这样的类

the actionperformerd()function call every 1second.

actionperformerd()函数每 1 秒调用一次。

Timer time = new Time(10,this);
.
.
.
public void actionperformed()
{
        timer.run;
        //i want move a pic every 1millisecond.
}

my problem is that Timerclass only accept int value and it's minimum value is 1 second and i want call actionperformed every 1 millisecond.

我的问题是Timer该类只接受 int 值,它的最小值是 1 秒,我希望每 1 毫秒调用一次 actionperformed。

回答by Oleg Pavliv

Java Timeraccepts milliseconds in parameters. So you can do

Java Timer在参数中接受毫秒。所以你可以做

new Timer().schedule(new TimerTask() {
public void run()  {
  // do stuff
}
}, 1, 1);

But to have real-time functionality with milliseconds precision you may need to switch to C.

但是要获得毫秒精度的实时功能,您可能需要切换到 C。

回答by khotyn

Try some classes from java.util.concurrent, and ScheduledThreadPoolExecutor can do the thing you want to do:

试试 java.util.concurrent 中的一些类,ScheduledThreadPoolExecutor 可以做你想做的事情:

public static void main(String[] args) throws Exception {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
    executor.schedule(new Runnable() {
        @Override
        public void run() {
            // Do something here.
        }
    }, 1, TimeUnit.MILLISECONDS);
}

BTW, the timer class can't run a job periodically accurately, it can only create one thread to run the task.

顺便说一句,定时器类不能准确地定期运行作业,它只能创建一个线程来运行任务。

回答by Vincent Mimoun-Prat

You could use a separate Thread

您可以使用单独的线程

class MyThread extends Thread {
  public void run() {
    while (!interrupted()) {
      try {
        // move my object, then sleep for a millisecond
        sleep(1);
      } catch (InterruptedException e) {
      }
    }
  }
}

However, in practice, you will rarely manage to have you move function called every 1 ms because other threads are also consuming processor time. So you need to take into account the actual time between the end of the previous thread loop and the current time.

然而,在实践中,您很少会设法让您每 1 毫秒调用一次移动函数,因为其他线程也在消耗处理器时间。所以你需要考虑上一个线程循环结束和当前时间之间的实际时间。

I suggest you read lots of tutorials about "Game Loops", you'll learn how to organise the functions moving objects, rendering, ...

我建议您阅读大量有关“游戏循环”的教程,您将学习如何组织移动对象、渲染、...

This one is an interesting article. Made for Android but can be applied to standard Java.

这是一篇有趣的文章。专为 Android 设计,但可应用于标准 Java。

回答by extraneon

If this happens to be something graphical be aware that you actually update the screen in the EDT (event dispatch thread). The GUI is notmultithreaded.

如果这恰好是图形化的东西,请注意您实际上更新了 EDT(事件调度线程)中的屏幕。GUI不是多线程的。

By hammering your EDT with updates in 1 ms intervals (even worse if you do this per pic) you might in effect make the GUI unusable - it is busy redrawing is stead of responding to user input.

通过以 1 毫秒的间隔更新 EDT(更糟的是,如果您按图片执行此操作),您实际上可能会使 GUI 无法使用 - 它忙于重绘而不是响应用户输入。

I really don't know whether that effect occurs 1 ms intervals, but the single threaded design of the GUI is something to take into account.

我真的不知道这种效果是否以 1 毫秒的间隔发生,但 GUI 的单线程设计是需要考虑的。