java定时器源代码,定时 java
编写一个java定时器 每隔5秒钟向数据库表中添加一条数据 求助应该咋写啊 求具体代码
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("在此处调用插入数据库的方法");
}
}
求一个简单的Java定时器源代码
我是让它每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();
ca1.add(Calendar.SECOND, 20);
}
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{
System.out.println("耗时"+String.valueOf(s)+"秒");
}
ca.add(Calendar.MILLISECOND, 10);
timer.schedule(
new TimerTask() {
public void run() {
System.out.println("时间在溜走。。。。");
Time();
}
}, ca.getTime());
}
void getcon(){
Connection con=null;//获取Connection
chu();
Time();
}
java写一个定时器,定时对一个变量赋不同值,这个程序代码怎么写
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() {
@Override
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();
}
}
求java的计时器代码,应该比较简单的,来看看吧。
package test;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test5 extends 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 {
public void actionPerformed(ActionEvent e) {
if(btnGo.isEnabled()){
return ;
}
if (btnPouse.getLabel().equals("继续")) {
swThread.go();
btnPouse.setLabel("暂停");
} else if (btnPouse.getLabel().equals("暂停")) {
swThread.noGo();
btnPouse.setLabel("继续");
}
}
}
private class btnResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("开始");
btnPouse.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) {
System.out.println(time+" "+((0*60+2)*1000+999));
if(((99*60+59)*1000+983)=time((99*60+59)*1000+999)=time){//((0*60+2)*1000+983)=time((0*60+2)*1000+999)=time
if (time % 1000 990)
time += 2;
swThread.noGo();
}
String ms, sec, min;
if (time % 10 = 5)
time += 5;
ms = Long.toString(time % 1000);
while (ms.length() 3)
ms = "0" + ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0" + sec;
time /= 60;
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() {
going = false;
prevElapsed = 0;
}
public void run() {
while (true) {
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new GridLayout(3,2));
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();
}
public static void main(String[] args) {
Test5 applet = new Test5();
Frame aFrame = new Frame("计时器");
aFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(400, 200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改变有注释的那个if语句里面的值来判断什么时候停止
java定时器
import java.io.IOException;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);// 在1秒后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
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 {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("________");
}
}
}
这段代码基本能满足你需求了 你还有需求就在上面再套一层job 当然如过太复杂了而且这种定时需求很多的话 建议用quartz框架 使用很简单
网站题目:java定时器源代码,定时 java
本文路径:http://hbruida.cn/article/hsedid.html