Creating an infinite loop in java

Associate
Joined
30 Jul 2007
Posts
996
Location
Surrey
Hi,

Basically im new to programming and as part of my course at uni i have to code a graphical clock.

I have done everything (and checked it works) except for managing a infinite loop bit of code so that upon starting the clock the following lines of code (for the minute hand continually repeat themselves:

clock.wait(60000);
minutes.rotate(6);

Any help/advice would be greatly appreciated :)
 
I'm just going through a Java book now and have come across an example where they make a clock like this (which is way too advanced for me). If you want me to quickly scan the pages (there are 2 iirc) then add me on msn (off out now, will be back at 6ish).
 
while(true){do stuff}

Not sure if you need to do other stuff at the same time, if so you'll need to thread it.
 
Last edited:
also, don't count on the wait timer to decide when to move the hands, by all means use it, but always check the actual time and use that instead as Java is not realtime and 60000 isn't guaranteed to be exactly 60s.
 
If I were doing it I'd use a thread that sleeps for 1000ms and then checks the time and sets the minute hand accordingly.

So for instance (not exact code, can't recall the syntax perfectly):

Code:
Thread t_checkTime;

public static void main(String[] args)
{
    t_checkTime = new Thread(checkTime());
    t_checkTime.Start();
}

private void checkTime()
{
    //Set minute hand of clock to System minute time

    Thread.Sleep(1000);
}

The problem, as said above, with having the main method wait is that you won't be able to do anything in the mean time. It's just a dirty way of coding and you should use this as a chance to learn about threading with this pretty simple example. The thread itself will wait and do everything in the background but you can still go ahead and do whatever draw or other update methods are needed in the meantime. If for instance you just had the main thread wait then it wouldn't redraw for those 60 seconds, that becomes a problem if you alt-tab out then back in and that sort of thing.
 
Last edited:
Back
Top Bottom