導航:首頁 > 編程語言 > java定時調用

java定時調用

發布時間:2022-09-28 13:20:35

Ⅰ 用java怎麼是實現定時調用某一個方法

/**
* 普通thread
* 這是最常見的,創建一個thread,然後讓它在while循環里一直運行著,
* 通過sleep方法來達到定時任務的效果。這樣可以快速簡單的實現,代碼如下:
* @author GT
*
*/
public class Task1 {
public static void main(String[] args) {
// run in a second
final long timeInterval = 1000;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// ------- code for task to run
System.out.println("Hello !!");
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}

Ⅱ java中怎麼定時調用方法

知道的有這三種實現方法 普通thread實現2.TimerTask實現3.ScheledExecutorService實現 一、普通thread 這是最常見的,創建一個thread,然後讓它在while循環里一直運行著,通過sleep方法來達到定時任務的效果。這樣可以快速簡單的實現

Ⅲ java里thread怎麼實現定時調度

java Thread類實現定時調度,可以延遲幾秒之後再執行,代碼如下:


publicclassceshi{
publicstaticvoidmain(String[]args)throwsException{
//runinasecond
finallongtimeInterval=1000;
Runnablerunnable=newRunnable(){
@Override
publicvoidrun(){
while(true){
//-------codefortasktorun
System.out.println("Hello!!");
//-------endshere
try{
Thread.sleep(timeInterval);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
};
Threadthread=newThread(runnable);//線程創建
thread.start();//線程啟動

}

}

運行結果:

閱讀全文

與java定時調用相關的資料

熱點內容
戰錘40kpdf 瀏覽:375
java判斷字元串是漢字 瀏覽:462
2017初級教材pdf 瀏覽:451
松下空調壓縮機品牌 瀏覽:817
python復選框 瀏覽:746
反詐中心app是什麼時候出來的 瀏覽:586
魔獸世界需要什麼伺服器地址 瀏覽:189
啥是單片機休眠 瀏覽:104
什麼音樂app最好 瀏覽:927
牙膏自製解壓神器 瀏覽:327
卸載linuxwps 瀏覽:226
threadjavarunnable 瀏覽:358
一元線性回歸計演算法 瀏覽:950
刷寶app是什麼鬼 瀏覽:744
linux動態載入模塊 瀏覽:25
javamd5工具類 瀏覽:76
安保嘉獎命令 瀏覽:647
bt種子能壓縮嗎 瀏覽:676
JAVA特點是什麼 瀏覽:886
字幕網app怎麼找 瀏覽:533