Multi threaded game - updating, rendering, and how to split them

后端 未结 7 1629
故里飘歌
故里飘歌 2021-02-10 10:54

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

7条回答
  •  礼貌的吻别
    2021-02-10 11:17

    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.

提交回复
热议问题