2012-07-29 71 views
11

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

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); 

    } 
}; 
+1

http://www.ibm.com/developerworks/java/library/j-schedule/index。html – nullpotent 2012-07-29 06:10:06

+0

你确定你正在创建一个'temperatureUp'线程并调用'start()'吗?这段代码适合我。 – 2012-07-29 06:12:10

+0

为什么你会同时使用线程和计时器?定时器运行在它自己的线程上 – 2012-07-29 06:16:28

回答

1

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

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

你可以在这里参考Timer类定义为提供进一步的帮助

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

+0

用'increaserTimer.schedule(increasetemperature,2000,2000)';'仍然没有任何反应! – Afflatus 2012-07-29 06:48:01

+0

@Elham - 我没有注意到它,但你从另一个线程中产生一个新的线程?这是一个非常错误的方法。考虑改变你的结构,以便产生线程并从全局级别进行调度。下面是一个示例项目的链接,它可以帮助你改变你的结构:换句话说,我不能使用Timer来调度你的结构:http://www.roseindia.net/java/example/java/util/CertainAndRepeatTime.shtml – afrin216 2012-07-29 06:56:03

+0

一系列由线程完成的动作。对? – Afflatus 2012-07-29 07:04:11

2

为了做一些事情,你应该使用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(); 
    } 
} 
15

在您的代码段有几个误区:

  • 您扩展Thread类,它是不是真的好做法
  • 你有ThreadTimer?这没有什么意义,因为Timer自己运行Thread

你还是(当/如果需要),实现一个Runnable看到here一个简单的例子,但是我看不到在你给的片段既是ThreadTimer的需要。

请参阅工作Timer的下面的例子中,这将只是一个一个地被调用时(每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将仅通过1每3秒递增counter,并且Thread将显示counter价值睡眠每它检查计数器时间(它将终止本身和counter==3后所述定时器)1秒时:

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 
    } 
} 
2
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++; 
    } 

}