2012-07-29 48 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++; 
    } 

}