import java.util.Timer;
import java.util.TimerTask;
public class Test extends TimerTask {
public static void main(String[] args) {
Timer timer = new Timer();
Test t=new Test();
//程序运行后立刻执行任务,每隔1000ms执行一次
timer.schedule(t, 0, 1000);
}
@Override
public void run() {
System.out.println("在此处调用插入数据库的方法");
我是让它每10毫秒扫描一下.
Calendar ca=null;
Calendar ca1=null;
Timer timer=new Timer();
static Connection con=null;
public void chu(){
ca=Calendar.getInstance();
ca1=(Calendar)ca.clone();
public void Time(){
float s=ca1.get(Calendar.SECOND)-ca.get(Calendar.SECOND)◆(ca1.get(Calendar.MILLISECOND)-ca.get(Calendar.MILLISECOND))/1000f;
if(con==null){
System.out.println("耗时"◆String.valueOf(s)◆"秒");
if(ca.after(ca1)){
System.out.println("取不到连接");
timer.cancel();
return ;
}else{
ca.add(Calendar.MILLISECOND, 10);
timer.schedule(
new TimerTask() {
System.out.println("时间在溜走....");
Time();
}, ca.getTime());
void getcon(){
Connection con=null;//获取Connection
chu();
import?java.util.Random;
import?java.util.Timer;
import?java.util.TimerTask;
public?class?MainEntry?{
private?int?a;
public?void?setVal(){
Timer?timer?=?new?Timer();
//每隔一秒生成一个[1,100)内的随机整数,赋给成员a
timer.schedule(new?TimerTask()?{?
public?void?run()?{
Random?rand?=?new?Random();
setA(rand.nextInt(100));
},?1000);
public?void?setA(int?a)?{
this.a?=?a;
public?int?getA()?{
return?a;
public?static?void?main(String[]?args)?{
MainEntry?me?=?new?MainEntry();
me.setVal();
package?test;
import?java.util.*;
import?java.awt.*;
import?java.awt.event.*;
import?java.applet.*;
private?final?Panel?pan?=?new?Panel();
private?final?Label?time?=?new?Label();
private?final?Button?btnGo?=?new?Button("开始");
private?final?Button?btnPouse?=?new?Button("暂停");
private?final?Button?btnReset?=?new?Button("复位");
private?final?StopwatchThread?swThread?=?new?StopwatchThread();
private?class?btnGoListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
swThread.go();
btnGo.setEnabled(false);
private?class?btnPouseListener?implements?ActionListener?{
if(btnGo.isEnabled()){
return?;
if?(btnPouse.getLabel().equals("继续"))?{
btnPouse.setLabel("暂停");
}?else?if?(btnPouse.getLabel().equals("暂停"))?{
swThread.noGo();
btnPouse.setLabel("继续");
private?class?btnResetListener?implements?ActionListener?{
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("开始");
private?class?StopwatchThread?extends?Thread?{
private?boolean?going?=?false;
private?long?prevElapsed?=?0;
private?Date?startDate?=?new?Date();
private?long?elapsedTime()?{
return?prevElapsed?◆
(going?new?Date().getTime()?-?startDate.getTime()?:?0);
private?String?msToString(long?time)?{
String?ms,?sec,?min;
ms?=?Long.toString(time?%?1000);
ms?=?"0"?◆?ms;
ms?=?ms.substring(0,?ms.length()?-?1);
time?/=?1000;
if?(sec.length()?==?1)?sec?=?"0"?◆?sec;
min?=?Long.toString(time);
return?min?◆?":"?◆?sec?◆?"."?◆?ms;
public?void?go()?{
startDate?=?new?Date();
going?=?true;
public?void?noGo()?{
prevElapsed?=?elapsedTime();
going?=?false;
public?void?reset()?{
prevElapsed?=?0;
while?(true)?{
time.setText(msToString(elapsedTime()));
yield();
public?void?init()?{
setBackground(Color.lightGray);
setForeground(Color.black);
pan.add(new?Label("计时:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new?Label());
add(pan);
btnGo.addActionListener(new?btnGoListener());
btnReset.addActionListener(new?btnResetListener());
btnPouse.addActionListener(new?btnPouseListener());
swThread.setDaemon(true);
swThread.start();
Frame?aFrame?=?new?Frame("计时器");
aFrame.addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
});
aFrame.add(applet,?BorderLayout.CENTER);
applet.init();
applet.start();
aFrame.setVisible(true);
可以改变有注释的那个if语句里面的值来判断什么时候停止
import?java.io.IOException;
public?class?TimerTest?{
while?(true)?{//?这个是用来停止此任务的,否则就一直循环执行此任务了
try?{
int?ch?=?System.in.read();
if?(ch?-?'c'?==?0)?{
timer.cancel();//?使用这个方法退出任务
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
static?class?MyTask?extends?java.util.TimerTask?{
//?TODO?Auto-generated?method?stub
System.out.println("________");
这段代码基本能满足你需求了 你还有需求就在上面再套一层job 当然如过太复杂了而且这种定时需求很多的话 建议用quartz框架 使用很简单
以上就是土嘎嘎小编为大家整理的java定时器源代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!