怎么让Java每过10分钟执行段程序? 请教高手 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // 提供一思路class CeiExport {private static CeiExport ce = new CeiExport();public static CeiExport getInstance(){return ce;}private CeiExport(){System.out.println("CeiExport 正在构建中... 构建完毕!");}public void printHello(){System.out.println("你好,朋友");} // 添加此对象的各种操作 .... }public class RemindTask extends TimerTask{public void run() { // CeiExport s=new CeiExport(); CeiExport s = CeiExport.getInstance(); //返回此对象的最新状态}public static void main(String[] args){Timer timer = new Timer(); timer.schedule(new RemindTask(), 0, 1*2000);}} import java.util.Timer;import java.util.TimerTask;/** * Simple demo that uses java.util.Timer to schedule a task to execute * once 5 seconds have passed. */public class Reminder { Timer timer; public Reminder(int seconds) { timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000); } class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); timer.cancel(); //Terminate the timer thread } } public static void main(String args[]) { System.out.println("About to schedule task."); new Reminder(5); System.out.println("Task scheduled."); }}或:Timer(int delay, ActionListener listener)ActionListener //使用线程public class TaskThread extends Thread { public void run() { try { while(true) { //执行操作 sleep(10 * 60 * 1000); } } catch (Exception e) { } }}public class MainClass { public static void main(String[] args) { //启动线程 new TaskThread.start(); }} to mydeman(漫步者)那怎么样让用户按Ctrl键退出呢?? You can wirte ower thread class to implement the feature.other one way is using TimerTask class. 给你个简单的例子private class RemindTask extends TimerTask{ public void run() { ...//要做的写这里. } }timer = new Timer();timer.schedule(new RemindTask(),0, interval*1000*60);//每隔INTERVAL分钟做一次. 我也喜欢用三楼的方法,结构比较清晰,或者直接sleep也可以. 三楼的调用的地方应该写成: TaskThread th = new TaskThread(); th.start(); 呵呵,谢谢指出,我也是随便copy一段代码,没有细查 to chalsy(白梓健): 谢谢,不小心写错了,应该是 new TaskThread().start(); Java 编写的计算器出错 Java冒泡排序降序 java中怎么实现组件随对话框的大小改变 如何判断当前月的第一天是星期几? 一道面试题 请教知道圆心和半径,画圆,有没有相应的方法?就是圆心合半径作为参数. 关于JTable显示数据的问题!顶者有分! java.net.socketpermission的问题 equals方法 如何把int类型的转化成String类型的 DEP2是什么文件类型啊?是怎么生成的,有什么用,怎样打开他它?高手帮忙!谢谢! Servlet的小程序!
class CeiExport
{
private static CeiExport ce = new CeiExport();public static CeiExport getInstance()
{
return ce;
}private CeiExport()
{
System.out.println("CeiExport 正在构建中... 构建完毕!");
}public void printHello()
{
System.out.println("你好,朋友");
} // 添加此对象的各种操作 ....
}public class RemindTask extends TimerTask
{
public void run()
{
// CeiExport s=new CeiExport();
CeiExport s = CeiExport.getInstance(); //返回此对象的最新状态
}
public static void main(String[] args)
{
Timer timer = new Timer();
timer.schedule(new RemindTask(), 0, 1*2000);
}
}
import java.util.TimerTask;/**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/public class Reminder {
Timer timer; public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
} class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //Terminate the timer thread
}
} public static void main(String args[]) {
System.out.println("About to schedule task.");
new Reminder(5);
System.out.println("Task scheduled.");
}
}或:
Timer(int delay,
ActionListener listener)ActionListener
public class TaskThread extends Thread {
public void run() {
try {
while(true) {
//执行操作
sleep(10 * 60 * 1000);
}
} catch (Exception e) {
}
}
}public class MainClass {
public static void main(String[] args) {
//启动线程
new TaskThread.start();
}
}
private class RemindTask extends TimerTask{
public void run() {
...//要做的写这里.
}
}
timer = new Timer();
timer.schedule(new RemindTask(),0, interval*1000*60);//每隔INTERVAL分钟做一次.
TaskThread th = new TaskThread();
th.start();
谢谢,不小心写错了,应该是 new TaskThread().start();