Java线程中的计时器

我有一个负责做一些过程的线程。 我希望这样做,以便这些处理每3秒完成一次。 我已经使用了下面的代码,但是当线程启动时,没有任何反应。 我假设当我为我的计时器定义一个任务时,它会在一个时间间隔内自动执行ScheduledTask ,但它根本不做任何事情。 我错过了什么?

 class temperatureUp extends Thread { @Override public void run() { TimerTask increaseTemperature = new TimerTask(){ public void run() { try { //do the processing } catch (InterruptedException ex) {} } }; Timer increaserTimer = new Timer("MyTimer"); increaserTimer.schedule(increaseTemperature, 3000); } }; 

您的代码段中有一些错误:

  • 你扩展了Thread类,这不是一个很好的练习
  • 你有一个ThreadTimer ? 当Timer在自己的Thread上运行时,这没有意义。

你应该(在必要的时候/必要时)实现一个Runnable 在这里看一个简短的例子,但是我看不到你给出的代码片段中是否需要ThreadTimer

请看下面一个工作Timer例子,它每次调用时都会将计数器递增1(每3秒):

 import java.util.Timer; import java.util.TimerTask; public class Test { static int counter = 0; public static void main(String[] args) { TimerTask timerTask = new TimerTask() { @Override public void run() { System.out.println("TimerTask executing counter is: " + counter); counter++;//increments the counter } }; Timer timer = new Timer("MyTimer");//create a new Timer timer.scheduleAtFixedRate(timerTask, 30, 3000);//this line starts the timer at the same time its executed } } 

附录:

我做了一个简单的例子,将一个Thread加入到混音中。 所以现在TimerTask只会每3秒递增1次counter ,并且Thread将在每次检查计数器时显示counter值hibernate1秒(它将自动终止并且计数器在counter==3 ):

 import java.util.Timer; import java.util.TimerTask; public class Test { static int counter = 0; static Timer timer; public static void main(String[] args) { //create timer task to increment counter TimerTask timerTask = new TimerTask() { @Override public void run() { // System.out.println("TimerTask executing counter is: " + counter); counter++; } }; //create thread to print counter value Thread t = new Thread(new Runnable() { @Override public void run() { while (true) { try { System.out.println("Thread reading counter is: " + counter); if (counter == 3) { System.out.println("Counter has reached 3 now will terminate"); timer.cancel();//end the timer break;//end this loop } Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } }); timer = new Timer("MyTimer");//create a new timer timer.scheduleAtFixedRate(timerTask, 30, 3000);//start timer in 30ms to increment counter t.start();//start thread to display counter } } 
 import java.util.Timer; import java.util.TimerTask; public class ThreadTimer extends TimerTask{ static int counter = 0; public static void main(String [] args) { Timer timer = new Timer("MyTimer"); timer.scheduleAtFixedRate(new ThreadTimer(), 30, 3000); } @Override public void run() { // TODO Auto-generated method stub System.out.println("TimerTask executing counter is: " + counter); counter++; } } 

为了每三秒做一次,你应该使用scheduleAtFixedRate(参见javadoc )。

但是你的代码确实什么也没做,因为你创建了一个线程,你在线程运行停止之前启动一个计时器(没有更多的事情可做)。 当计时器(单次拍摄)触发时,没有线程中断(之前完成运行)。

 class temperatureUp extends Thread { @Override public void run() { TimerTask increaseTemperature = new TimerTask(){ public void run() { try { //do the processing } catch (InterruptedException ex) {} } }; Timer increaserTimer = new Timer("MyTimer"); //start a 3 seconds timer 10ms later increaserTimer.scheduleAtFixedRate(increaseTemperature, 3000, 10); while(true) { //give it some time to see timer triggering doSomethingMeaningful(); } } 

我认为你使用的方法有签名schedule(TimerTask task, long delay) 。 所以实际上你只是推迟了ONLY执行的开始时间。

要安排它每3秒运行一次,您需要使用此方法schedule(TimerTask task, long delay, long period) ,其中第三个参数用于给出周期间隔。

您可以在此处参考Timer类定义以获得进一步的帮助

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html