2016-12-08 2 views
-4

백그라운드에서 실행되는 프로그램을 만들려고합니다. 특정 시간에 실행되면 컴퓨터에 미리 알림이 나타납니다. 나는 그것이 usertime 시간 = 때까지 프로그램을 실행 유지 있도록 만들려고 노력하고Java - 진실을 기다리십시오.

int looplol = 2; 
while(looplol != 1){ 
    if(usertime.equals(time)){ 
     JOptionPane.showMessageDialog(null, usertext); 
     looplol = 1; 
    } 

는, 다음은 사용자가 원하는 메시지를 표시하고 프로그램을 중지합니다. 이 코드는 작동하지 않습니다. 누구든지이 작업을 수행 할 수있는 방법을 알고 있습니까?

+3

usertime와 시간은 무엇인가 후 ONCE 일을 하시나요? 그들은 문자열입니까? 더 많은 코드를 보여주세요. – XtremeBaumer

답변

0

이 코드는 조건에 도달 할 때까지 CPU 코어를 100 %로 회전시킵니다.

현재 시간과 "사용자 시간"(밀리 초) 사이의 시간을 계산할 수 있다면 Thread.sleep(ms)을 사용하지 않는 이유는 무엇입니까?

long userTime = <some time in the future>; 
long sleepTime = System.currentTimeMillis() - userTime; 

try { 
    Thread.sleep(sleepTime); 
} catch(InterruptedException ex) { 
    // Shouldn't happen 
} 
0

당신은 단순히 Thread.sleep() 사용할 수 있습니다 다음

private void waitUntilSystemTimeMillis(long stopTime) { 
    long sleepDuration = stopTime - System.currentTimeMillis(); 
    if (sleepDuration > 0) { 
     try { 
      Thread.sleep(sleepDuration); 
     } 
     catch(InterruptedException e) { 
      throw new RuntimException(e); 
     } 
    } 
} 

그리고 작업을 수행합니다 또한

waitUntilSystemTimeMillis(time); 
JOptionPane.showMessageDialog(null, usertext); 

참조 : https://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html

0

자바 패키지 폴더의 유틸리티 타이머를 가지고 ... 거기 당신은 객체를 정의 할 수 있고 주어진 경우 지연 후 메소드 호출 ...

당신은 사용할 수 있습니다 Timer.schedule을 지연

Timer t = new Timer("--", true); 
t.schedule(new TimerTask() { 

    @Override 
    public void run() { 
     JOptionPane.showMessageDialog(null, "usertext"); 
    } 
}, 5000L); 
관련 문제