Tag: 计时器

如何使用Java中的计时器在特定时间内运行作业?

我正在开发一个Web应用程序,我需要运行一个60秒的线程,需要检查来自Web服务的响应。 如果响应在60秒内到达,我将转发成功,然后我将在60秒后转发到超时页面。 我在使用JSF 2.0? 我曾想过使用Timer但不确定我是否只能在sprcefic的时间内运行计时器。 这有什么聪明的解决方案?

如何以指定的时间间隔定期自动调用servlet

必须定期调用servlet。(如andorid中的服务概念) 这该怎么做。 使用计时器还是其他解决方案? 提前致谢。

键盘Actionlistener

我试图让我的游戏中的太空飞船(PlayerShip.gif)在按下相应的键时向左,向右,向上和向下移动。 我知道我需要一个keyboardListener,但我有问题弄清楚它到底去了什么以及它实际上是如何实现的。 我的代码如下。 public class GamePanel extends JPanel implements KeyListener { Timer timer1; Timer timer2; ArrayList ship; int x; int y; double speed; int size; int shipxCoord; int shipyCoord; int shipHeight; int shipWidth; int shipRise; int shipRun; boolean left = false; boolean right = false; boolean up = false; boolean down = false; Image enemyShip1; […]

我可以使用Timer对象使程序饱和吗?

我有一个程序,我计划使用很多Timer对象,据我所知,每个Timer都运行在它自己的线程上。 所以我想知道是否有可能启动这么多的计时器,它会伤害程序的性能太multithreading。 例如,我想有几个(布尔,计时器)对可以在几个不同的时间间隔反转布尔值。

如何制作定时器?

我想制作一个等待400 MSc的Timer ,然后去打印“hi!” (例如)。 我知道如何通过javax.swing.Timer来做到这一点 ActionListener action = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(“hi!”); } }; 加: timer = new Timer(0, action); timer.setRepeats(false); timer.setInitialDelay(400); timer.start(); 但据我所知,这绝对不是一个好方法,因为这种Timer适用于Swing。 如何以正确的方式做到这一点? (不使用Thread.sleep() )

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

我有一个Android应用程序,它有一个运行任务的计时器: time2.scheduleAtFixedRate(new TimerTask() { @Override public void run() { sendSamples(); } }, sampling_interval, sending_interval); 让我们说sampling_interval是2000,sending_interval是4000。 因此,在此应用程序中,我将一些读取值从传感器发送到服务器。 但我想在10000(10秒)后停止发送。 我该怎么办?

Java中的resettable超时

(类似于“Resettable Java Timer”,但我需要探索一些细微之处) 我需要一个可重置的超时function,这样如果我的类在时间间隔T0内没有执行特定动作(其中T0在50-1000毫秒附近),则会调用一个方法: class MyClass { static final private timeoutTime = 50; final private SomeTimer timer = new SomeTimer(timeoutTime, new Runnable () { public void run() { onTimeout(); }}); private void onTimeout() { /* do something on timeout */ } public void criticalMethod() { this.timer.reset(); } } 我可以用什么来实现这个? 我熟悉ScheduledExecutorService ,并且调用ScheduledFuture.cancel()然后重新安排任务的想法似乎应该可以工作,但是如果cancel()失败并且计划任务在不应该执行时执行则存在潜在危险。 我觉得我在这里错过了一个微妙的东西。 另外(也许更重要的是),有没有办法测试我的实现/certificate它正常工作? 编辑:我特别关注的是经常调用criticalMethod()的情况(可能每毫秒几次)…如果我使用ScheduledExecutorService,那么继续创建新的计划任务+取消旧任务似乎是一个潜在的资源问题,而不是直接重新安排任务。

计时器错误java.lang.IllegalStateException

我试图在JRE 8中显示一个标签,说明所有内容都是正确的3秒,因为我无法在JRE 7中使用DatePicker而且我发现了这个错误。 Exception in thread “Timer-2” java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-2 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source) at javafx.scene.Scene.addToDirtyList(Unknown Source) at javafx.scene.Node.addToSceneDirtyList(Unknown Source) at javafx.scene.Node.impl_markDirty(Unknown Source) at javafx.scene.Node$7.invalidated(Unknown Source) at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source) at javafx.beans.property.BooleanPropertyBase.set(Unknown Source) at javafx.css.StyleableBooleanProperty.set(Unknown Source) at javafx.scene.Node.setVisible(Unknown Source) at Pantallas.PersonalContabilidad$1.run(PersonalContabilidad.java:221) at java.util.TimerThread.mainLoop(Unknown Source) at java.util.TimerThread.run(Unknown Source) correct是一个标签,我显示3秒,它在correct.setVisible(false);失败correct.setVisible(false); […]

重新启动ScheduledExecutorService计划任务的正确方法是什么?

我有一个计划任务(在固定延迟执行中运行),像这样开始: executoreService.scheduleWithFixedDelay(repeatingThread, 0, numOfSeconds, TimeUnit.SECONDS); 在每个循环开始时,我检查设置文件中的更改,然后我想重新启动任务。 设置文件还包含间隔的长度(上面代码中的numOfSeconds )。 目前,我使用以下代码重新启动任务: executoreService.shutdownNow(); try { while(!executoreService.awaitTermination(5, TimeUnit.SECONDS)){ logger.debug(“awaiting termintation”); } } catch (InterruptedException e) { logger.debug(“interrupted, continuing”, e); } // initialize startup parameters init(); // start the main scheduled timer executoreService.scheduleWithFixedDelay(repeatingThread, 0, numOfSeconds, TimeUnit.SECONDS); 我不确定这些API调用。 重启任务的推荐方法是什么(可能有新的延迟)?

Java的Timer任务是否保证不会同时运行?

new Timer(…).schedule(task) task保证在任何给定时间由单个线程运行吗?