Question

I have written a 2D Raycaster that can draw a ray to any java shape, including:

Line2D, Rectangle2D, Ellipse2D, QuadCurve2D, and CubicCurve2D.

The math behind it is actually pretty complicated and intensive, especially for quads and cubics, so i set up a thread pool. I made an ExcecutionService "exec" with a fixed thread count based on the number of available cores.

I then changed the chunks of code responsible for each shape into static runnables. From the raycaster method, I'd execute the runnables then call exec.shutdown(). finally, id create a while loop that doesn't exit until exec.isTerminated returns true.

This ensures that all of the threads are done executing before continuing.

Now, from what i've seen online, this is a popular way of doing it and yet I'm not seeing improved results. It's always the same if not worse. Is there a proper way to use thread pools that actually increases efficiency instead of matching is up with overhead?

Était-ce utile?

La solution

As @Adam Siemion said, firstly make sure your PC isn't readily bounded by CPU and you do have a multi-core CPU. Otherwise you cannot be faster anyway.

Secondly, don't create threads dynamically. Use a thread pool with a task queue (See ExecutorService etc in java.util.concurrent). If you are constantly creating threads for large number of small tasks then the thread creation and cleaning up would occupy some considerable amount of time.

Thirdly, I recommend to make sure your threads ends at the same time, Use stuff like CyclicBarrier or CountDownLatch, instead of waiting within the threads on your own.

If the above doesn't resolve your problem, please kindly post some code here. Another guess is you have some shared resource serial dependency, e.g. the painting thread etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top