本文介绍了用于指令的Java Timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想问你们一个问题。可以在for循环中初始化java计时器吗?这样定时器将使用从ArrayList获取的值运行一次。下面这个想法:



Hello there,
I want to ask you people a question. Can a java timer be initialized in a for loop? So that the timer will once run once with a value taken from an ArrayList. Something like the idea bellow:

  void myTimerMethod(){

   int size=ArrayList.getSize();
   Timer timer=new Timer();

   for(int i=0;i>size;i++){

     timer.schedule(new TimerTask(){

         public void run(){

           //do something here for a specified amount of time.
           //let's say display a character in console.

         }


     },ArrayList.get(i));  //set the timer to run only once, for a value taken from ArrayList.

   }


}







我想要要知道是否可以做到这样的事情。如果是的话,请你就如何实施这样的方法给我一个建议:)?...



非常感谢。




I want to know if something like this can be done. If yes, could you please give me an advice on how to implement such a method :) ?...

Thank you very much.

推荐答案

public class MyTimer extends Timer{

  private final strID; // final -> has to be set by constructor

  public MyTimer(String strID){
    this.strID = strID;
  }

  public String getID(){
    return strID;
  }
}





因此您可以在运行时稍后识别定时器。

一个数字也可以作为ID。



So you can identifify the Timer later on during runtime.
A number would do also fine as ID.


这篇关于用于指令的Java Timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:24