请教高手一个问题,虽然我已无分可用,但请DX们务必帮忙啊!! Timer,或者Thread.sleep(1000)//一秒 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 转贴。在应用开发中,经常需要一些周期性的操作,比如每5分钟检查一下新邮件等。对于这样的操作最方便、高效的实现方式就是使用java.util.Timer工具类。比如下面的代码每5分钟检查一遍是否有新邮件: private java.util.Timer timer; timer = new Timer(true); timer.schedule(new java.util.TimerTask() { public void run() { //server.checkNewMail(); 检查新邮件 } }, 0, 5*60*1000);使用这几行代码之后,Timer本身会每隔5分钟调用一遍server.checkNewMail()方法,不需要自己启动线程。Timer本身也是多线程同步的,多个线程可以共用一个Timer,不需要外部的同步代码。 在《The Java Tutorial》中有更完整的例子:public class AnnoyingBeep { Toolkit toolkit; Timer timer; public AnnoyingBeep() { toolkit = Toolkit.getDefaultToolkit(); timer = new Timer(); timer.schedule(new RemindTask(), 0, //initial delay 1*1000); //subsequent rate } class RemindTask extends TimerTask { int numWarningBeeps = 3; public void run() { if (numWarningBeeps > 0) { toolkit.beep(); System.out.println("Beep!"); numWarningBeeps--; } else { toolkit.beep(); System.out.println("Time's up!"); //timer.cancel(); //Not necessary because we call System.exit System.exit(0); //Stops the AWT thread (and everything else) } } } ...}这段程序,每隔3秒响铃一声,并打印出一行消息。循环3次。程序输出如下:Task scheduled.Beep! Beep! //one second after the first beepBeep! //one second after the second beepTime's up! //one second after the third beepTimer类也可以方便地用来作为延迟执行,比如下面的代码延迟指定的时间(以秒为单位)执行某操作。类似电视的延迟关机功能。...public class ReminderBeep { ... public ReminderBeep(int seconds) { toolkit = Toolkit.getDefaultToolkit(); timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000); } class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); toolkit.beep(); //timer.cancel(); //Not necessary because we call System.exit System.exit(0); //Stops the AWT thread (and everything else) } } ...} 高分悬赏:怎样把一个jframe程序转为applet程序? java算法的一道题(面试) 监听器的问题 关于Applet的问题,请教高手! 如何使一个线程停了再多次用 javaSE Jasperreport报表 为什么ArrayList的迭代比LinkedList更慢? 消费者生产者问题 关于InetAddress.getByName 征求jRedioButton的简单使用方法 修改Look And Feel 后如何使JFrame和ContentPanel面板上的控件更新 请问关于Toolkit.getDefaultToolkit().getScreenSize()的问题
private java.util.Timer timer;
timer = new Timer(true);
timer.schedule(new java.util.TimerTask() {
public void run() {
//server.checkNewMail(); 检查新邮件
}
}, 0, 5*60*1000);使用这几行代码之后,Timer本身会每隔5分钟调用一遍server.checkNewMail()方法,不需要自己启动线程。Timer本身也是多线程同步的,多个线程可以共用一个Timer,不需要外部的同步代码。
在《The Java Tutorial》中有更完整的例子:
public class AnnoyingBeep {
Toolkit toolkit;
Timer timer; public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
} class RemindTask extends TimerTask {
int numWarningBeeps = 3; public void run() {
if (numWarningBeeps > 0) {
toolkit.beep();
System.out.println("Beep!");
numWarningBeeps--;
} else {
toolkit.beep();
System.out.println("Time's up!");
//timer.cancel(); //Not necessary because we call System.exit
System.exit(0); //Stops the AWT thread (and everything else)
}
}
}
...
}
这段程序,每隔3秒响铃一声,并打印出一行消息。循环3次。程序输出如下:
Task scheduled.
Beep!
Beep! //one second after the first beep
Beep! //one second after the second beep
Time's up! //one second after the third beepTimer类也可以方便地用来作为延迟执行,比如下面的代码延迟指定的时间(以秒为单位)执行某操作。类似电视的延迟关机功能。
...
public class ReminderBeep {
...
public ReminderBeep(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
} class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
toolkit.beep();
//timer.cancel(); //Not necessary because we call System.exit
System.exit(0); //Stops the AWT thread (and everything else)
}
}
...
}