calendar - Constantly check time in Java -


so digging around in old java projects never finished , pulled out little number of best projects ever built.

enter image description here

it's desktop clock widget coded in java , works fine except 1 thing. way have check current time stay updated in loop , loop "crashes" in matter of seconds widget no longer gets current time.

this how loop constructed (reduced in size):

public class getcurrenttime {     public static void gettime() throws interruptedexception {         int hour = global.calendar.get(calendar.hour);         int minute = global.calendar2.get(calendar.minute);          if(hour == 0) {             global.hour.seticon(new imageicon("resources/hours/12.png"));         }else if(hour == 23) {             global.hour.seticon(new imageicon("resources/hours/11.png"));         }else {             global.hour.settext("?");         }          if(minute == 0) {             global.minute.seticon(new imageicon("resources/minutes/00.png"));         }else if(minute == 59) {             global.minute.seticon(new imageicon("resources/minutes/59.png"));         }else {             global.minute.settext("?");         }         thread.sleep(1000);         gettime();     } } 

global class keep of variables (i know it's weird, 3 years ago, it's how used write programs).

so main question is, there way can prevent loop "crashing"?

thanks in advance!

thread.sleep(1000); not going option. can use timer. using timer schedule task @ regular intervals.

  timer timer = new timer();   long interval = (1000) ; // 1 sec    timer.schedule( new timertask() {        public void run() {           //do work;        }   }, 0, interval);  

if want stop scheduling, can use timer.cancel();

edit: fildor said think memory problem. use timer way, should solve problem.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -