Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing objects between multiple threads in a game

Programming language: JAVA / Android

Thread "Structure"

Main Thread (starts other treads)

---(multiple attributes)

---AI thread

---TouchListener thread

---Scripting thread

---Render thread

Description

An example for an attribute would be an actor that is instructed by the scritping thread; has it's route calculated by the AI thread and the 3D coordinates changed by the renderer.

All the threads are NOT private inner classes with access to the attributes, instead they are simple classes which implement Runnable

Question

How to share objects (the attributes) between those endlessly running threads? Every thread must have access to all the resources of the main thread. (the question is not about how to syncronize them, I know "syncronized" and the concept of locks already)

like image 519
gurkenmaster1000 Avatar asked Aug 06 '26 14:08

gurkenmaster1000


1 Answers

You may create a class SimulationModel which is instantiated by Main thread/class and provided to others by a setter or by their constructor.

This class contains all the data and own the locks to maintain consistency.

The logic about data manipulation may take place here too.

like image 180
Aubin Avatar answered Aug 08 '26 16:08

Aubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!