1 回答
拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
/**?
*?普通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();??
}??
}??
添加回答
舉報
0/150
提交
取消
