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)

  • Assumingly the "multiple attributes" are held by an object - why don't you pass that object to the "classes that implement Runnable"? – assylias Oct 30 '12 at 15:59
  • I am confused since as I understand it, *threads* don't share or own objects, but rather objects use other objects. – Hovercraft Full Of Eels Oct 30 '12 at 16:01
  • I guess you can use [`AtomicReference`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicReference.html) Which which will be shared across threads and which will contain object to be shared. – Amit Deshpande Oct 30 '12 at 16:19

3 Answers3

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.

Aubin
  • 14,617
  • 9
  • 61
  • 84
  • 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? – gurkenmaster1000 Oct 30 '12 at 16:12
  • 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. – gurkenmaster1000 Oct 30 '12 at 16:26
  • 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. – gurkenmaster1000 Oct 30 '12 at 16:53
  • I suggest you to learn UML (http://en.wikipedia.org/wiki/Unified_Modeling_Language) to draw the entire application on a small piece of paper with a pen. Old fashion... – Aubin Oct 30 '12 at 17:11
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.

John B
  • 32,493
  • 6
  • 77
  • 98
  • 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. – gurkenmaster1000 Oct 30 '12 at 16:09
  • They refer to the one same object. No copies are involved. – Hovercraft Full Of Eels Oct 30 '12 at 16:24
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;
Community
  • 1
  • 1
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306