Threads and Graphics

My program has two threads. One calls repaint every 1/2 of a second (using the sleep command), and the other does everything else. The thread that calls repaint is not regular. It’s very sporadic, and there’s no pattern. I get a different timing even when I run the program again. However, when I add System.out.println() right before the sleep command, the problem is fixed. Anybody know of this problem?

Thanks.

Aye, that’s just generally how threads work. Don’t do your painting in a separate thread.

Cas :slight_smile:

If you work with repaint(), very often you have two threads. Normale the painting thread (Swing) idles so that the repaint()-calling thread is ready to run when sleep terminates.
But under some conditions, when there are more than one thread with same priotity that are ready to run, there’s not guarantee that a certain one of these does actually run at all. Maybe this is what you see.

Try to give your repaint()-calling thread a higher priority and make sure it sleeps most of the time.