I've been messing around with NetBeans creating a clock that is always on top and always loads in the bottom right of the screen. I've gotten it completed however I think my code is eating away at memory, after I left it over night I came back to 1.4GB of memory being used and a large amount of the CPU being used. I'm new to Java programming so I'm hoping this isn't normal!
In my main thread I create a new Calendar object each run. Moving this out of the Thread creates a Calendar object that is initialised with the current time but never updates, is there a better method of doing this?
I'm not used to dealing with Threads and I think I may have gotten turned around. Can anyone suggest improvements to the following that will lower my memory footprint and cpu usage?
public JavaClockGUI() {
initComponents();
new Thread()
{
public void run()
{
while(true)
{
Calendar cal = new GregorianCalendar();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
int sec = cal.get(Calendar.SECOND);
String time = String.format("%02d",hour) + ":" + String.format("%02d",min) + ":" + String.format("%02d",sec);
lblTime.setText(time);
}
}
}.start();
}