社区
Java SE
帖子详情
如何实现每30秒就执行一段程序?
peppi
2002-12-26 02:03:00
假设我有一方法:
public void a()
{
System.out.println("testing");
}
我有多少种方法可以让它每隔30秒就执行一次?永不停止!
请重点说明如何利用线程来实现,谢谢!最好有代码.
...全文
122
8
打赏
收藏
如何实现每30秒就执行一段程序?
假设我有一方法: public void a() { System.out.println("testing"); } 我有多少种方法可以让它每隔30秒就执行一次?永不停止! 请重点说明如何利用线程来实现,谢谢!最好有代码.
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
deniswang
2002-12-26
打赏
举报
回复
线程无法保证时间准确,最好用Timer
松耦合紧内聚
2002-12-26
打赏
举报
回复
觉得beyond_xiruo(希偌)的方法正规!标准计时器的使用。
线程睡30秒不是准确时间.
SnowOct
2002-12-26
打赏
举报
回复
支持一下
jackal81
2002-12-26
打赏
举报
回复
同意 wang_zheng_wz(流星雨) 的说法
希偌
2002-12-26
打赏
举报
回复
interface TimerListener{
public void processEvent();
}
public Class Clock implements TimerListener{
Clock(){
Timer t=new Timer(this); //向Timer类登记
}
public void processEvent(){
//你的事件处理的代码
}
}
class Timer extends Thread {
private TimerListener tl;
Timer(TimerListener tl){
this.tl=tl;
}
public void run(){
while(true){
sleep(1000);
tl.processEvent();
}
}
}
wang_zheng_wz
2002-12-26
打赏
举报
回复
用线程:
import java.io.*;
class AThread extends Thread {
private boolean stop = false;
public void terminate() {
stop = true;
}
public AThread() {
this.start();
}
public void run() {
while (!stop) {
try {
sleep(30 * 1000);
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
System.out.println("testing");
}
}
}
public class Test {
public static void main(String[] args) throws IOException {
AThread a = new AThread();
System.out.println("Press enter");
System.in.read();
a.terminate();
}
}
不用线程:
用java.util.Timer
deniswang
2002-12-26
打赏
举报
回复
用timer
希偌
2002-12-26
打赏
举报
回复
用线程
public void a() throws Exception
{
while(true) {
System.out.println("testing");
Thread.sleep(30000);
}
}
一个TSR
程序
作出DOS下的屏幕保护
程序
TSR
程序
可以
实现
各种功能,包括自动
执行
任务、管理系统资源、监控硬件状态等。在这个特定的案例中,【标题】"一个TSR
程序
作出DOS下的屏幕保护
程序
"展示了如何利用TSR技术在古老的DOS环境下创造一种屏幕保护机制。 ...
在JAVA怎样
实现
每间隔
一段
时间
执行
程序
当需要每间隔
一段
时间
执行
一段
程序
的时,JAVA中可以这样
实现
:public void run() { while (true) { try { xxxxxxxxxxxxxxxxx; // 需要
执行
的
程序
sleep(1000L); //间隔1
秒
执行
一次! } catch (Exception e) ...
c++
实现
每隔1
秒
钟
执行
一段
程序
#include #include using namespace std; int getTime() { return clock()/CLOCKS_PER_SEC; } int main() { int i = 0; int lastTime = 0; while (1) { int now = getTime();... if (now - lastTim
每隔
一段
时间自动
执行
一次某个方法(使用线程)
c#里,怎么让一个方法事件每隔
一段
时间自动
执行
一次?能用线程来
实现
吗?怎么
实现
? 比如:SendToService这个方法,要每隔10
秒
钟自动
执行
一次。 //定义线程 Thread LogThread = new Thread(new Thread...
php输出调整每行间隔,
实现
php间隔
一段
时间
执行
一次某段代码
ignore_user_abort();...//
执行
时间为无限制,php默认
执行
时间是
30
秒
,可以让
程序
无限制的
执行
下去$interval=24*60*60;//每隔一天运行一次do{sleep($interval);//按设置的时间等待一小时循环
执行
$sql="updateblog...
Java SE
62,623
社区成员
307,257
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章