如何在JAVA中每隔一段时间执行一段程序
1个回答
展开全部
可以用线程来做,每隔几秒开一个线程
代码如下
public
void
runTask()
{
final
long
timeInterval
=
120000;//
两分钟运行一次
final
ThreadService
threadService
=
new
ThreadService();
Runnable
runnable
=
new
Runnable()
{
public
void
run()
{
while
(true)
{
//
-------
code
for
task
to
run
//你要运行的程序
//
-------
ends
here
try
{
Thread.sleep(timeInterval);
}
catch
(InterruptedException
e)
{
e.printStackTrace();
}
}
}
};
Thread
thread
=
new
Thread(runnable);
thread.start();
}
代码如下
public
void
runTask()
{
final
long
timeInterval
=
120000;//
两分钟运行一次
final
ThreadService
threadService
=
new
ThreadService();
Runnable
runnable
=
new
Runnable()
{
public
void
run()
{
while
(true)
{
//
-------
code
for
task
to
run
//你要运行的程序
//
-------
ends
here
try
{
Thread.sleep(timeInterval);
}
catch
(InterruptedException
e)
{
e.printStackTrace();
}
}
}
};
Thread
thread
=
new
Thread(runnable);
thread.start();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询