Java Runnable中重复任务的最佳解决方案

标签 java swing jframe eclipse-neon

您会建议我做什么来实现我的目标? 我想重复一遍Robot定期进行操作(按下按键)。说明也在我的代码中。 我正在使用eclipse neon with windowbuilder (java/swing/jframe)。

package patrick;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.sql.Time;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import javax.management.timer.Timer;

public class Background implements Runnable {

    public boolean isRunning = true;
    int intervall = 0;
    Robot r = null;


    public Background(int i)
    {
        this.intervall = i;
    }




    @Override
    public void run() 
    {
        System.out.println("Thread: " + Thread.currentThread().getName() + " gestartet!");
        System.out.println("Intervall i: " + intervall);
        try {
            r = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        while(isRunning)
        {
            //int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
            //now I want to do Robot stuff here that repeats in the given intervall time
        }
    }

    public void stop()
    {
        isRunning = false;
        System.out.println("Thread: " + Thread.currentThread().getName() + " gestoppt!");
    }

}

编辑:

后台是从 MainWindow 类启动的,如下所示:

bg = new Background(itmp);
        th1 = new Thread(bg);
        th1.start();

EDIT2:第一个答案

像这样吗?

timer.scheduleAtFixedRate(
while(isRunning)
        {
            //int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
            //now I want to do Robot stuff here that repeats in the given intervall time
        }
, delay, period);

编辑3:

timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                while(isRunning)
                {
                    //int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
                    //now I want to do Robot stuff here that repeats in the given intervall time
                }
            }
        }, 0, intervall*6000);

最佳答案

我会说 Timer可能非常适合您的需求。例如,使用public void Schedule(TimerTask task, long delay),您可以设置一个在几毫秒后执行任务的计时器。所以在你的情况下 300000 毫秒(5 分钟)和 TimerTask .

long delay = 300000L;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        //do something
    }
}, delay);

关于Java Runnable中重复任务的最佳解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40345899/

相关文章:

java - 在ubuntu中使用命令行执行jar时出现问题

java - 移动 JButton

java - 安卓开发 : How do I integrate threads in this?

Java ArrayList 删除元素恢复浪费的空间?

java - 从 JList 中检索特定项目

java - 带菜单栏的 swing JFrame 上的投影

java - 在按键上删除 JList 项目的异常

java - 当 JasperViewer 出现并且我关闭它时,主框架/父框架也关闭了

Java GUI 调出上一个窗口

java - Swing GUI - 创建一个 'Settings' 窗口(正确)