java - 如何在每次运行 TimerTask 时更改 timer.schedule() 的周期?

标签 java random timer

timer.schedule(new PressTask(), 2000, rand);

我希望上面的 rand 每次执行时都是不同的数字。例如,第一次调用 timer.schedule 时,假设 rand 是 5345。下次调用它时,它应该是一个不同的数字,而不是 5345。我该怎么做?

请原谅乱七八糟的代码,这只是我为自己做的一个小练习。

public class Keypress 
{
    static Timer timer = new Timer();
    static JFrame frame = new JFrame();
    static JButton btn = new JButton("start");
    static JButton btn2 = new JButton("stop");
    static JTextField txt = new JTextField();
    static Robot robot;
    static Random couch = new Random();
    static int rand = couch.nextInt(10000 - 5000) + 5000;

   public static void main(String[] args) 
    {
System.out.println(rand);

frame.setSize(500,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn.addActionListener(new startPress());
btn2.addActionListener(new stopPress());
    frame.setLayout(new GridLayout(3, 1));
frame.add(btn);
frame.add(btn2);
frame.add(txt);

try 
{
    robot = new Robot();
} 
catch (AWTException e) 
{
    e.printStackTrace();
}
frame.setVisible(true);
}
static class startPress implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        timer.schedule(new PressTask(), 2000, rand);
    }
}
public static class PressTask extends TimerTask
{
public void run()
{
    robot.keyPress(KeyEvent.VK_1);
    robot.keyRelease(KeyEvent.VK_1);
}
}
static class stopPress implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        System.exit(0);
    }
}
}

最佳答案

ScheduledExecutorService怎么样?而不是 Timer

public ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
public Random rand = new Random();    

class Repeater implements Runnable{
    private Runnable task;
    private long interval;
    private long maxInterval;
    private TimeUnit unit;
    public Repeater(Runnable task, long maxInterval, TimeUnit unit){
        this.task = task;
        this.interval = (rand.nextLong())%maxInterval //don't let interval be more than maxInterval
        this.unit = unit;
        this.maxInterval = maxInterval;
    }

    public void run(){
        executor.schedule(new Repeater(task,maxInterval,unit)
            , interval, unit);
    }
}

class startPress implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        int maxIntervalMs = 5000;
        executor.schedule( new Repeater(new PressTask(),initialIntervalMs,TimeUnit.MILLISECONDS)
            , rand.nextLong()%maxIntervalMs, TimeUnit.MILLISECONDS);
    }
}

注意:您实际上可以使用 Timer 而不是 ScheduledExecutorService 来做同样的事情,但是精彩的书“Java Concurrency in Practice”出于多种原因,建议使用 ScheduledExecutorService 而不是 Timer,例如,如果从任务中抛出异常,则可以更好地处理以及更灵活的 api。

关于java - 如何在每次运行 TimerTask 时更改 timer.schedule() 的周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34377472/

相关文章:

java - Wicket 和多线程业务对象

c++ - 倒数计时器线程

java - 打乱字符串时防止重复字母?

java - 发送嵌入 SOAP 消息中的 XML 数据的最佳实践是什么?

java - Java中有没有一种方法可以生成遵循固定均值和标准差的随机数?

c# - Random.Next() - 找到第 N 个 .Next()

java - 如何创建一个对象来启动一个与我的程序一起运行的线程?

java - 参数传递java

java - ArrayList 根据 pojo 对象查找字段

c# - 创建(真实)随机迷宫的最佳逻辑