jjzjj

scheduleAtFixedRate

全部标签

java - scheduledThreadPool.scheduleAtFixedRate() 奇怪的行为

我有这个简单的测试:importjava.util.Timer;importjava.util.TimerTask;publicclassScheduleTest{privatestaticlonglast;publicstaticvoidmain(String[]args){last=System.currentTimeMillis();Timertimer=newTimer();timer.schedule(newTimerTask(){@Overridepublicvoidrun(){Longcurrent=System.currentTimeMillis();System.ou

java - executorService.scheduleAtFixedRate 永远运行任务

我希望任务在1分钟的间隔后永远运行。为了完成我在中写下的任务publicvoidpoll(){ScheduledExecutorServiceexecutorService=Executors.newScheduledThreadPool(1);ScheduledFuturefiles=executorService.scheduleAtFixedRate(newRunnable(){@Overridepublicvoidrun(){Stringpath="/Desktop/PNL/Test";ListfilesPaths=newArrayList();Filefolder=newFi

Java 定时器和 scheduleAtFixedRate + 系统挂起

我正在开发一个Java程序并使用Timer对象每隔几分钟或几小时运行一次任务。这在正常操作中工作正常,但我在Mac上遇到了“sleep模式”的问题(可能在其他操作系统上,但我还没有尝试过)。考虑这个代码示例://Setupthetimertofirethepingworker(every3minutes)_PingTimer.scheduleAtFixedRate(newTimerTask(){publicvoidrun(){Program.PingThread=newPingWorker(Settings.Username,Settings.UserHash,true,true);P

java - Java 的 ScheduledExecutorService.scheduleAtFixedRate() 行为

我有一个关于scheduleAtFixedRate()的问题Java6中ScheduledExecutorService的方法。[编辑:1.6的Javadoc比1.5的更完整。请参阅下面的评论]鉴于:ScheduledExecutorService是在池中使用N=1个线程构建的固定速率是T秒的周期没有初始延迟在这种情况下会发生什么(在实时意义上,时间并不是绝对的):在时间T,服务启动一个Runnable任务,“task1”在时间2T,task1尚未完成,服务计划启动服务是否保证执行以下任何操作?(a)在2T,启动一个Runnable任务,“task2”(召回N=1)(b)阻塞直到tas

java - 如何在一定时间后重复调用一个函数

我想制作一个在一定时间后调用的函数。此外,这应该在相同的时间后重复。例如,该函数可能每60秒调用一次。 最佳答案 使用java.util.Timer.scheduleAtFixedRate()和java.util.TimerTask是一个可能的解决方案:Timert=newTimer();t.scheduleAtFixedRate(newTimerTask(){publicvoidrun(){System.out.println("hello");}},0,//runfirstoccurrenceimmediatetly2000))

java - 为什么 scheduleAtFixedRate - scheduleWithFixedDelay 方法不使用 Callable<V>

我正在做一些关于Java8并发性的实验在ScheduledThreadPoolExecutor接口(interface)我可以看到以下两个签名:schedule(Callablecallable,longdelay,TimeUnitunit)schedule(Runnablecommand,longdelay,TimeUnitunit)一个用于Callable,一个用于Runnable我也可以在API中看到以下两个:scheduleAtFixedRate(Runnablecommand,longinitialDelay,longperiod,TimeUnitunit)scheduleW

Java "scheduleAtFixedRate"替代解决方案?

我有一个Java应用程序,用于通过UART连接(RS422)与嵌入式设备通信。主机以5毫秒为间隔向微Controller查询数据。直到最近我一直在使用ScheduledExecutorServicescheduleAtFixedRate来调用我的通信协议(protocol)方法,但事实证明scheduleAtFixedRate对于这种所需的精度级别非常不可靠(正如许多其他帖子所揭示的那样)。从微Controller返回的数据中有一个时间戳(以微秒为单位),使我能够独立于JVM验证接收到的数据包之间的间隔。不用说,使用scheduleAtFixedRate时的间隔变化很大——数据包之间最

android - 即使用户没有使用我的应用程序也更新 TextView

我基本上想创建一个计时器。我有一个TextView,我需要在其中显示一个计时器,例如“在2分29秒内更新您的位置”,我希望计时器递减,例如2分28秒,然后是2分27秒。即使用户没有使用我的应用程序,我也想更新它(我不希望它在真正意义上更新,我的意思是当用户在说1分钟后打开我的应用程序时,计时器应该显示1分27秒).有人可以帮我指出正确有效的方法吗?谢谢:) 最佳答案 我认为最有效的方法是使用scheduleAtFixedRate当您的Activity处于前台并且当它进入后台时您不需要任何服务您只需将当前时间和剩余时间保存在共享首选项

java - 如何在一定时间后停止计时器?

我有一个android应用程序,它有一个运行任务的计时器:time2.scheduleAtFixedRate(newTimerTask(){@Overridepublicvoidrun(){sendSamples();}},sampling_interval,sending_interval);假设sampling_interval是2000,而sending_interval是4000。所以在这个应用程序中,我将一些读取值从传感器发送到服务器。但我想在10000(10秒)后停止发送。我该怎么办? 最佳答案 尝试time2.sche

java - 延迟执行Java方法中的代码

我想在我的java(Android)程序中每2秒生成一次随机数,持续至少10分钟。但我只想在一种方法中而不是整个程序中暂停/延迟代码的执行。我试过像这样使用Thread-booleanstop=false;intrandom_number=0;while(true){if(stop){//ifstopbecomestrue,thenreturn;//terminatethemethod}random_number=Math.random();//generaterandomnumber//whichisusedbusomeother//partofcodetry{Thread.slee