java利用Timer和TimerTask進行定時調度時,如果碰到TimerTask的任務執行時間多於定時調度的時間頻率則會出現延遲狀況,所以在TimerTask的run方法中通過開闢線程的方法來對任務進行處理,減少了時間延遲帶來的誤差
1. [代碼]每次調度信息都會被顯示在控制檯和被寫到文件裏面,可以直接看控制檯的數據也可以到文件上去查看信息
package
timer;
import
java.io.File;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
java.util.TimerTask;
import
thread.EventRunnable;
import
file.TxtFileUtil;
public
class
RunTimerTask
extends
TimerTask
{
public
static
int
num =
1
;
File
file =
new
File(
"C:\\Users\\Administrator.BGQJFNVC3FDF6KO\\Desktop\\14.txt"
/**
文件路徑名 **/
);
@Override
public
void
run() {
double
startTime
= System.currentTimeMillis();
System.out.println(
"start"
);
/**
*
每次調用將日期存入文件中
*/
Date
date =
new
Date();
SimpleDateFormat
simpleDateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd
HH:mm:ss"
);
String
stringDate = simpleDateFormat.format(date);
TxtFileUtil.appendToFile(stringDate
+
"\r\n"
,
file);
/**
*
每次執行一個時間調度任務時都開闢一個線程去執行,這樣可以減少時間開銷避免任務執行時間過長導致定時調度的延遲
*/
EventRunnable
eventRunnable =
new
EventRunnable(
"event"
+
num);
Thread
thread =
new
Thread(eventRunnable);
thread.start();
/**
*
算出執行完run方法後消耗的總時間,如果超過一秒則定時器將延遲
*/
double
endTime
= System.currentTimeMillis();
System.out.println(
"RunTimerTask"
+
num+
":"
+
(endTime - startTime) +
"毫秒"
);
TxtFileUtil.appendToFile(
"RunTimerTask"
+num
+
":"
+
(endTime - startTime)
+
"毫秒"
+
"\r\n"
,
file);
num++;
while
(RunTimerTask.num
==
11
)
{
this
.cancel();
}
}
}
package
thread;
import
java.io.File;
import
file.TxtFileUtil;
public
class
EventRunnable
implements
Runnable
{
private
String
name;
File
file =
new
File(
"C:\\Users\\Administrator.BGQJFNVC3FDF6KO\\Desktop\\14.txt"
/**
文件路徑名 **/
);
public
EventRunnable(String
name) {
super
();
this
.name
= name;
}
@Override
public
void
run() {
double
startTime
= System.currentTimeMillis();
String
x =
"nihao"
;
for
(
int
i
=
0
;
i <
80000
;
i++) {
x
+=
"!"
;
}
double
endTime
= System.currentTimeMillis();
System.out.println(name
+
":"
+
(endTime - startTime) +
"毫秒"
);
TxtFileUtil.appendToFile(name
+
":"
+
(endTime - startTime) +
"毫秒"
+
"\r\n"
,
file);
}
}
package
timer;
import
java.io.File;
import
file.TxtFileUtil;
import
java.io.ObjectInputStream.GetField;
import
java.sql.Time;
import
java.util.Timer;
public
class
testTimeTast {
public
static
void
main(String[] args) {
File
file =
new
File(
"C:\\Users\\Administrator.BGQJFNVC3FDF6KO\\Desktop\\14.txt"
/**
*
*
文件路徑名
**/
);
try
{
TxtFileUtil.createFile(file);
}
catch
(Exception
e) {
e.printStackTrace();
}
/**
*
新建一個時間調度任務
*/
RunTimerTask
run =
new
RunTimerTask();
Timer
timer =
new
Timer();
timer.schedule(run,
0
,
1000
);
}
}
package
file;
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.FileReader;
import
java.io.FileWriter;
import
java.io.IOException;
import
java.math.BigInteger;
import
java.security.MessageDigest;
/**
*
*
@author nyc
*
*
用於讀寫txt文件的工具類
*/
public
class
TxtFileUtil {
/**
*
創建文件
*
*
@param txtFile
*
@return
*/
public
static
boolean
createFile(File txtFile)
throws
Exception
{
boolean
flag
=
false
;
try
{
if
(!txtFile.exists())
{
txtFile.createNewFile();
flag
=
true
;
}
}
catch
(Exception
e) {
e.printStackTrace();
}
return
flag;
}
/**
*
讀TXT文件內容
*
*
@param txtFile
*
@return
*/
public
static
String readTxtFile(File txtFile)
throws
Exception
{
String
result =
""
;
FileReader
fileReader =
null
;
BufferedReader
bufferedReader =
null
;
try
{
fileReader
=
new
FileReader(txtFile);
bufferedReader
=
new
BufferedReader(fileReader);
try
{
String
read =
null
;
while
((read
= bufferedReader.readLine()) !=
null
)
{
if
(!read.equals(
"\r\n"
)){
result
= result + read +
"\r\n"
;
}
else
{
result=result+read;
}
}
}
catch
(Exception
e) {
e.printStackTrace();
}
}
catch
(Exception
e) {
e.printStackTrace();
}
finally
{
if
(bufferedReader
!=
null
)
{
bufferedReader.close();
}
if
(fileReader
!=
null
)
{
fileReader.close();
}
}
return
result;
}
public
static
boolean
compareFiles(File srcFile, File desFile) {
return
getFileMD5(srcFile).equals(getFileMD5(desFile));
}
private
static
String getFileMD5(File file) {
if
(!file.isFile()){
return
null
;
}
MessageDigest
digest =
null
;
FileInputStream
in=
null
;
byte
buffer[]
=
new
byte
[
1024
];
int
len;
try
{
digest
= MessageDigest.getInstance(
"MD5"
);
in
=
new
FileInputStream(file);
while
((len
= in.read(buffer,
0
,
1024
))
!= -
1
)
{
digest.update(buffer,
0
,
len);
}
in.close();
}
catch
(Exception
e) {
e.printStackTrace();
return
null
;
}
BigInteger
bigInt =
new
BigInteger(
1
,
digest.digest());
return
bigInt.toString(
16
);
}
/**
*
追加到內容到原文件尾部
*
*
@param txtFile
*/
public
static
boolean
appendToFile(String content, File txtFile) {
boolean
append
=
false
;
boolean
result
=
false
;
try
{
if
(txtFile.exists())
append
=
true
;
FileWriter
fw =
new
FileWriter(txtFile,
append);
BufferedWriter
bf =
new
BufferedWriter(fw);
bf.append(content);
result
=
true
;
bf.flush();
bf.close();
}
catch
(IOException
e) {
e.printStackTrace();
}
return
result;
}
/**
*
將內容寫到TXT文件,覆蓋原來內容
*
*
@param content
*
:寫入的符串
*
@param txtFile
*
:文本文件
*
@return: 是否寫入成功
*
@throws Exception
*
:拋出異常
*/
public
static
boolean
writeTxtFile(String content, File txtFile)
throws
Exception
{
boolean
flag
=
false
;
FileOutputStream
outStream =
null
;
try
{
outStream
=
new
FileOutputStream(txtFile);
if
(txtFile.exists())
{
txtFile.delete();
}
outStream.write((
new
String(
""
)).getBytes());
outStream.flush();
outStream.write(content.getBytes(
"utf8"
));
outStream.close();
flag
=
true
;
}
catch
(Exception
e) {
e.printStackTrace();
}
return
flag;
}
public
static
void
copyFile(File frmFile, File toFile) {
String
content =
""
;
try
{
content
= readTxtFile(frmFile);
if
(toFile.exists())
{
toFile.delete();
}
writeTxtFile(content,
toFile);
}
catch
(Exception
e) {
e.printStackTrace();
}
}
}