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

发布于 2024-10-19 13:43:46 字数 366 浏览 1 评论 0原文

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

例如,当我们使用 Time 类时,

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

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

我的问题是 Timer 类只接受 int 值并且它是最小值 值为 1 秒,我希望每 1 毫秒执行一次调用操作。

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

for example wen we use Time class like blew

the actionperformerd() function call every 1second.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天邊彩虹 2024-10-26 13:43:46

Java Timer 接受毫秒参数。所以你可以这样做

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

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

Java Timer accepts milliseconds in parameters. So you can do

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.

终难遇 2024-10-26 13:43:46

尝试一下 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);
}

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

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

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.

温馨耳语 2024-10-26 13:43:46

您可以使用单独的线程,

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

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

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

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

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.

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.

风为裳 2024-10-26 13:43:46

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

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

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文