JAVA每隔几个_Java每隔几秒就从循环内执行一次方法(Java execute method from within a loop every few seconds)...

Java每隔几秒就从循环内执行一次方法(Java execute method from within a loop every few seconds)

我有一个在我的测试应用程序中运行的线程,在线程内部有一个while循环。 while循环正在运行时,我希望每隔30秒从此while循环中执行一个方法。 在while循环中,不想让线程休眠或停止循环,它必须运行并且每30秒调用一次该方法。

Thread myThread = new Thread() {

@Override

public void run() {

//my code that runs with a loop

//while loop here that runs and needs to execute method every 30 seconds, if condition met continue else break;

};

myThread.start();

}

I have a Thread that runs in my test app with a while loop inside the thread. While the while loop is running i want to execute a method from within this while loop every 30 seconds. Inside the while loop, don't want to sleep the thread or stop the loop, it must run and every 30 seconds call the method.

Thread myThread = new Thread() {

@Override

public void run() {

//my code that runs with a loop

//while loop here that runs and needs to execute method every 30 seconds, if condition met continue else break;

};

myThread.start();

}

原文:https://stackoverflow.com/questions/23013560

2020-02-18 04:32

满意答案

等待,你可以使用

Thread.sleep(milliseconds);

有关文档,请参阅此处

如果你在循环中等待30秒,它会每30秒发生一次+你的功能执行时间。 只要您的函数调用只需要几毫秒,这就像以更复杂的方式执行它一样精确。

如果您希望循环继续运行但您不想启动新Thread ,则可以使用当前时间 :

long lastCall = 0;

while(bla) {

if(System.currentTimeMillis() - lastCall > 30000) {

lastCall = System.currentTimeMillis();

callTheFunction();

}

}

To wait, you can use

Thread.sleep(milliseconds);

For documentation, see here

If you wait for 30 seconds in your loop, it happens every 30 seconds + execution time of your function. As long as your function call only takes milliseconds this is as precise as doing it in a more complex way.

If you want your loop to keep running but you do not want to launch a new Thread, you can use the current Time:

long lastCall = 0;

while(bla) {

if(System.currentTimeMillis() - lastCall > 30000) {

lastCall = System.currentTimeMillis();

callTheFunction();

}

}

2014-04-11

相关问答

您可以使用“setInterval”函数。 这是一个例子: var interval1Id = setInterval(function(){

console.log("logging every 5 seconds");

},5000);

var interval2Id = setInterval(function(){

console.log("logging every 7 seconds");

},7000);

var interval3Id = setInterval...

最简单的是使用sleep 。 apple.x = rg.nextInt(470);

apple.y = rg.nextInt(470);

Thread.sleep(1000);

循环运行上面的代码。 这将给你一个近似 (可能不准确)的一秒延迟。 Simplest thing is to use sleep. apple.x = rg.nextInt(470);

apple.y = rg.nextInt(470);

...

一个完美的替代品不存在接缝,所以我将使用我疯狂的解决方案#1: $this->last_tick_time = microtime(true);

$this->tick_interval = 1;

$this->tick_counter = 0;

while(true)

{

//loop code here...

$t= microtime(true) - $this->last_tick_time;

if($t>= $this->tick_interval)

...

我发现我在类的顶部实例化任务并且每次都从同一个实例调用execute,但是如果我每次想要调用任务时都将任务设置为new,那就像魅力一样 I found that I was instantiating the task at the top of the class and calling the execute from that same instance each time, but if I made the task new each time I wanted to call the ...

你可以产生一个新的线程: void *threadproc(void *arg)

{

while(!done)

{

sleep(delay_in_seconds);

call_function();

}

return 0;

}

...

pthread_t tid;

pthread_create(&tid, NULL, &threadproc, NULL);

或者,您可以使用alarm(2)或setitimer(2)设置警报: voi...

使用NSTimer并将时间interval设置为2 seconds然后repeats为YES 。 计算它触发的次数。 当它达到10时Invalidate 。说明它 码: [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(trigger:) userInfo:yourObject repeats:YES];

- (void)trigger:(NSTimer *)sender{

id you...

您可以使用Handler执行此操作 public class Job implements Runnable{

private Handler handler;

public Job () {

handler = new Handler(Looper.getMainLooper());

loop();

}

@Override

public void run() {

// funky stuff

...

像mkdir和mv这样的命令是shell命令,并且要求您在执行它们时将它们放在shell中。 如果你把这些命令放在一个文件中,在顶部有适当的hashbang ,然后从java调用它,它应该可以工作。 但是,您可以在纯java中执行“for f in folder”部分,然后使用ProcessBuilder执行脚本 The commands like for, mkdir and mv are shell commands and require you to be in a shell when ...

等待,你可以使用 Thread.sleep(milliseconds);

有关文档,请参阅此处 如果你在循环中等待30秒,它会每30秒发生一次+你的功能执行时间。 只要您的函数调用只需要几毫秒,这就像以更复杂的方式执行它一样精确。 如果您希望循环继续运行但您不想启动新Thread ,则可以使用当前时间 : long lastCall = 0;

while(bla) {

if(System.currentTimeMillis() - lastCall > 30000) {

...

相关文章

1.1 导出数据的应用框架,考虑这样一个实际应用:实现一个导出数据的应用框架,来让客户选择数据的导出

...

3.2 工厂方法模式与IoC/DI,IoC——Inversion of Control 控制反转,

...

3.1 认识工厂方法模式(1)模式的功能 工厂方法的主要功能是让父类在不知道具体实现的情况下,完成自

...

3.3 平行的类层次结构(1)什么是平行的类层次结构呢?简单点说,假如有两个类层次结构,其中一个类层

...

pro-du-cer n. 1. Someone from a game publisher who

...

2.1 工厂方法模式来解决 用来解决上述问题的一个合理的解决方案就是工厂方法模式。那么什么是工厂方法

...

As you know, I've been playing with Solr lately, tr

...

Spring Data: a new perspective of data operations

...

Hibernate exception - Illegal attempt to associate

...

In a time 有一次 Where the sun descends alone, 太阳孤独的降落

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值