3

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)

3
  • Assumingly the "multiple attributes" are held by an object - why don't you pass that object to the "classes that implement Runnable"? Commented Oct 30, 2012 at 15:59
  • I am confused since as I understand it, threads don't share or own objects, but rather objects use other objects. Commented Oct 30, 2012 at 16:01
  • I guess you can use AtomicReference Which which will be shared across threads and which will contain object to be shared. Commented Oct 30, 2012 at 16:19

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But if I put the SimulationModel in the constructor of each thread the SimulationModel object isn't the same one (wouldn't be the same with the ===) across all threads, they would be local copies only, right?
Simply making the entire SimulationModel with static methods and attributes seems easier (for a game). (see answer from F.J.), but you got me on the right track.
Apparently I have no idea how Java works :D (4.5 years of learning it), I made a little demo and it worked (and I'm puzzled why :U ). Thank you, I will use that, thanks.
I suggest you to learn UML (en.wikipedia.org/wiki/Unified_Modeling_Language) to draw the entire application on a small piece of paper with a pen. Old fashion...
0

If you are not asking about making access to the objects thread-safe, are you just asking how to have access to them in the other threads? If so, just pass them in to those runnable objects via a constructor.

2 Comments

But they are just copies (different reference), right? The reference inside the constructor of a thread is not the same that was passed inside from the main thread, else it would be possible to change everything you get from a getter of any class.
They refer to the one same object. No copies are involved.
0

One option would be to make your "attributes" static variables in a class to use them globally:

Example from that answer:

public class Global {

public static int a;
public static int b;
}

now you can access a and b from anywhere by calling

Global.a;

Global.b;

3 Comments

Does this still count as an OOP concept? Or is it a tradeoff for being global?
No, this isn't exactly object oriented, but I think the only really object oriented way to solve this exact problem would be to have your threads all be inner classes.
Thank you, never tried to make a truly static class, now I have a new tool on my belt. (but I won't use it in this case)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.