So, I\'m working on a game engine, and I\'ve made pretty good progress. However, my engine is single-threaded, and the advantages of splitting updating and rendering into separa
Place your update logic in some kind of Updater
worker class (implementing Runnable
), and put renderer into separate worker class. When you need to update data, let Updater put that update into queue shared by both Updater and Producer. Most convenient would be to use queue which already have built-in multi-threaded support, like subclass of BlockingQueue
. For example code, see javadoc for BlockingQueue
.
Using queue is natural if you need to render all changes, even obsolete ones. If you wish to render only the latest change, use ConcurrentHashMap
instead of queue.
Don't forget to make your updates immutable objects, so there's no chance update can change while you render it.
As Nirmal pointed out, you could use some kind of thread pool to limit number of threads and to simplify starting/stopping of threads. Refer to Executor
interface and Executors
utility class in JDK to see available options here.