-1

I want to draw circles on the jpanel, where each circle should appear alternately on the random location with a different color and after for example 2000ms, it dies (disappears), and then appears a new one.

However, I want to use a thread pool with a 3 threads so that I can show 3 circles in the same time.

So now I'm not sure how should I execute CircleRunnable class and make this circles appear on the screen?

Xerath
  • 1,069
  • 5
  • 17
  • 26
  • what you *want* to do and what you **should** do is not in alignment, first lesson to learn –  Aug 03 '14 at 14:57
  • At the moment, your code will draw an arbitrary number of circles as it will paint *all* circles contained in `circleList` and your code lacks any lifetime management at all. So you are asking how to use *more* threads to implement drawing *less* circles (once you start creating some). It doesn’t make any sense. – Holger Aug 04 '14 at 11:21

1 Answers1

4

No, you shouldn't use multiple threads for this. Graphics isn't something that's generally multithreaded (as evidenced by Swing's Event/Paint thread).

Instead you should keep track of circles and their lifetimes in a suitable collection and handle that in a single thread. For example creating a javax.swing.Timer and generating circles randomly each tick.

That way you can have hundreds of circles, and you won't shoot yourself in the foot with multithreading in a Swing program.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • @Xerath - the first thing to learn about concurrency and threading is when **NOT** to use it. –  Aug 03 '14 at 14:55