Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrency issues and hashmap
    primarykey
    data
    text
    <p>I am starting to think about multithreading for my game and the best place to start would be to seperate the three main components of the game: logic, gfx, sfx, into 3 seperate threads. The problem is they all need to access the same <code>hashmap</code> of entities as the logic, gfx and sfx are completely seperate from eachother.</p> <pre><code>private Map&lt;String, Entity&gt; entities; private Map&lt;String, GameSound&gt; sounds; private Map&lt;String, ArrayList&lt;BufferedImage&gt;&gt; images; </code></pre> <p>The same hashmap is passed around to the different classes and object corruption is very likely with multiple threads. Currently the <code>SoundsEngine</code> is the one threaded (else the music pauses between <code>Thread.sleep()</code> and that is fine as it doesn't change, it just reads.</p> <p>Is there an easy (and not hardhitting on performance) way to implement a threadsafe <code>HashMap</code> without adding synchronized all over my code (and probably introducing more bugs this way)? I thought about taking a shallow copy every 60 frames as that is the maximum rate anything in my game will change and passing the copy around in the gameloop to the different engines but that seems a bity "hacky".</p> <p>The concurrency problem is show by the constructors in the factory:</p> <pre><code>public class Factory { private Map&lt;String, Entity&gt; entities; private Map&lt;String, GameSound&gt; sounds; private Map&lt;String, ArrayList&lt;BufferedImage&gt;&gt; images; private GameEventListener listener; private GameFrame frame; private ImageLoader Iloader; private SoundLoader Sloader; public Factory() { this.Iloader = new ImageLoader("imagelist.txt", images); this.Sloader = new SoundLoader("soundlist.txt", sounds); this.frame = new GameFrame(Color.black); this.listener = new GameEventListener(); } public GameEngine createGameEngine() { return new GameEngine(entities, images, sounds); } public SoundEngine createSoundEngine() { return new SoundEngine(entities, sounds); } public GraphicsEngine createGraphicsEngine() { return new GraphicsEngine(entities, frame); } } </code></pre> <p>Edit: Singletons not an option, already did that in a previous game, I want to learn a more "loose" design in which I can independently develop the engines without breaking something because I have the wrong state.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload