Do I need a new thread?
Hello,
I have a jpanel that I am displaying a clock on. I want the clock to update every minute.
I can do this with a while loop, but it blocks the rest of the program from running. Should
I start a new thread to update the clock or is there a better way to do it. A thread is
just another path of execution in the same process as the parent...right? So starting new
threads should not put to much stress on a system or ever reach a limit of threads spawable like starting a new process will?
Im still trying to decern a process from a thread.
thanks,
jd
[610 byte] By [
mohadib_a] at [2007-9-30 2:25:03]

When i first started using threads i felt it comfortable to liken the behaviour of a thread to a different program altogether. I know that that is an inaccurate way to think about it but threads run in "parallel" to the parent process like your word processor and your mp3 player run at the same time without blocking each other.
My apologies to the purists for this comparison, but simplifying my thoughts into analogies always helps me learn :)
Also, there's a nice bit of reference material at http://java.sun.com/docs/books/tutorial/essential/threads
- Mr K
> Take a look at javax.swing.Timer
Which creates a background thread that issues event calls.
Anyway, the answer to the OP's question is "Yes", the easiest way to do this is to have another thread, that spends most of it's time sleeping. Using the Timer class is a quick and dirty way to do this, so you don't have to worry (much) about the multi-threading issues involved.
Please keep in mind that you will need to use the invokeAndWait() or invokeLater() methods of the SwingUtilities class to update the UI from a thread other than the AWT event thread.
- K