2016-10-31 2 views
0

내 목표를 달성하기 위해 내가 무엇을 권하고 싶습니까? 정규 intervall에서 Robot 물건 (키 누름)을 반복하고 싶습니다. 설명도 내 코드에 있습니다. eclipse neon with windowbuilder (java/swing/jframe)을 사용 중입니다.Runnable에서 반복되는 작업을위한 Java 최고의 솔루션

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!"); 
    } 

} 

편집 :

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

EDIT2 :이 같은 MainWindow를 클래스에서

배경 시작이 같이

첫 번째 대답

에?

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); 

EDIT3는 :

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); 
+3

내가 EDIT2에서처럼 타이머 https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html – Chains

+0

을 찾고 당신의 동안 당신은 의미합니까 가치가있을 수도? – Patrick260284

+1

TimerTask 객체를 만들어야합니다. https://docs.oracle.com/javase/8/docs/api/java/util/TimerTask.html – Chains

답변

2

나는 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); 
+0

제 편집 3처럼? – Patrick260284

+1

@ Patrick260284, 예. –

+0

당신이 원하는 것들이 업데이트 GUI 컴포넌트를 실행한다면 AWT 타이머가 아닌 스윙 타이머를 사용해야한다. Swing 구성 요소에 대한 모든 업데이트는 Event Dispatch Thread에서 수행해야합니다. Swing Timer는 자동으로 EDT에서 실행됩니다. – camickr

관련 문제