所有類型的 Java 應用程序一般都需要計劃重復執行的任務
Timer類是用來執行任務的類
java
Timer
Timer最常用的是schedule執行任務的模式
看個簡單的例子:
Java代碼
import java
import java
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer
/*
//這個是用來停止此任務的
while(
try {
if(
timer
}
} catch (IOException e)
e
} */
}
static class Job extends java
@Override
public void run() {
// TODO Auto
System
}
}
}
import java
import java
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer
/*
//這個是用來停止此任務的
while(
try {
if(
timer
}
} catch (IOException e)
e
} */
}
static class Job extends java
@Override
public void run() {
// TODO Auto
System
}
}
}
TimerTask 剛才在代碼裡提到了 TimerTask 類
完整的代碼:
Java代碼
public class TimerTest{
public static void main(String[] args){
int delay=
Timer timer=new Timer();//生成一個Timer對象
NewTask myTask=new NewTask();//初始化我們的任務
timer
}
}
class NewTask extends TimerTask{//繼承TimerTask類
public void run(){
System
}
}
public class TimerTest{
public static void main(String[] args){
int delay=
Timer timer=new Timer();//生成一個Timer對象
NewTask myTask=new NewTask();//初始化我們的任務
timer
}
}
class NewTask extends TimerTask{//繼承TimerTask類
public void run(){
System
}
}這樣這個程序就會在五秒鐘後輸出 printing!
Timer的功能是相當強大的
完整案例!
現在大家看一下我寫的關於實現LED滾屏顯示動態信息!
web 監聽
Java代碼
package com
import java
import javax
import javax
import javax
import org
import com
//import com
public class LEDListener implements ServletContextListener {
private Timer timer = null;
private Logger log = Logger
// GetconfigInfor config = new GetconfigInfor();
GetInformation getInfo = new GetInformation(
Integer runTime = Integer
String fileRealPath =
String templePath =
public void contextInitialized(ServletContextEvent event) {
// 在這裡初始化監聽器
ServletContext context = event
fileRealPath = context
+ System
+ System
templePath = context
+ System
+ System
timer = new Timer();
(
timer
(
}
public void contextDestroyed(ServletContextEvent event) {// 在這裡關閉監聽器
timer
(
}
}
package com
import java
import javax
import javax
import javax
import org
import com
//import com
public class LEDListener implements ServletContextListener {
private Timer timer = null;
private Logger log = Logger
//GetconfigInfor config = new GetconfigInfor();
GetInformation getInfo = new GetInformation(
Integer runTime = Integer
String fileRealPath =
String templePath =
public void contextInitialized(ServletContextEvent event) {
// 在這裡初始化監聽器
ServletContext context = event
fileRealPath = context
+ System
+ System
templePath = context
+ System
+ System
timer = new Timer();
(
timer
(
}
public void contextDestroyed(ServletContextEvent event) {// 在這裡關閉監聽器
timer
(
}
}
LEDTimeTrack
Java代碼
package com
import java
public class LEDTimerTack extends TimerTask {
String fileRealPath =
String templePath =
String type=
int floor;
public LEDTimerTack(String fileRealPath
this
this
this
this
}
public LEDTimerTack(String fileRealPath
this
this
}
public void run( ) {
for(int i=
//呵呵
XlsUtil
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25938.html