Results 1 to 10 of 10
- 08-25-2010, 09:12 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
Passing Objects Between Threads -->
I am making a simulation in which objects are stored in a 2 dimentional array.
For the sake of this post you can think of the 2 dimentional array as being a soccer field and the objects in the array as the players.
When I first wrote this program each player acted one at a time. I just made a loop and called each players "act()" method on the field. This made things very easy.
I just pass the "field" object to each player via in the players "act()" method. So it would look like this --> player.act(field);
then each player knows exactly what is on the field and in what location. It then makes its move and passes the 2D array back to the field class and the updates are saved before passing the field on to the next player.
However, now I want each object (player) to run its own thread so every player on the field opperates independantly in real time.
Since each player is running its own thread I need a way to pass the field object to each player every time the field is updated, so every player is working from the latest field stats.
Ive been reading some tutorials about passing object between threads and saw some stuff about "piped input/output streams".
I am new to this technique and was wondering if this would work? Can I pass objects between the threads this way? I am hoping someone with more experiance can point me in the right direction. Maybe this is not the right approach. If anyone has any ideas on how to accomplish this please let me know.
thanks.
-
You don't pass objects between threads, and this doesn't make sense to me. You pass objects to other objects, same as always.
- 08-25-2010, 11:17 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
thats what im doing sort-of. sorry if my english doesnt make sence.
Im passing the "field" object to the "player" object. The player object implements runnable. Each player is running in a thread and the field object needs to be continuously updated by each player. The problem is that when the thread is run I have no way of passing or returning data between the player and the field.
Basically, I just need a way for each player to run independantly and in real time.
Other people have suggested using pipes but im not sure if taht will work. If anyone has any ideas I would be very greatfull.
- 08-26-2010, 10:33 AM #4
I agree with fubarable, it should be done in just the same way as normal.
You could do it in several ways.
A simple option would be to store a list / array of all the players. A seperate thread would go through the list and give each player the field. This is probably close to what you were doing before you started threading.
Secondly each player could store the field in a private variable. This could be passed through the constructor, and the "act" method would use the private variable.
This ofcourse assumes that a player only acts on one field at a time - which i think is the right assumption (correct me if im wrong!)
berkeleybross
- 08-26-2010, 11:06 AM #5
- 08-26-2010, 07:32 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
Thanks for the replies guys...
So, I pass the filed object to each player via their constructor (and each player is running their own thread). Since there is only one field object, when a player moves positions on the field (2d array) every other player on the field will know the current position of where he moved correct?
Does the 2d array have to be a public static variable, or can it be a private variable accessed only through methods? I assume that since there is only one field it can be private and not static (please correct me if I am wrong).
If this is correct, All I would ahve to then is syncronise all the threads and methods so players dont conflict with eachother and crash the program correct?\
EDIT:
If I each player has a variable that stors the field object wouldnt that mean that ever player would be working with a diffrent version od the field? I would need some way updating the current field stats for each player object?Secondly each player could store the field in a private variable. This could be passed through the constructor, and the "act" method would use the private variable.
This ofcourse assumes that a player only acts on one field at a time - which i think is the right assumption (correct me if im wrong!)Last edited by shuks; 08-26-2010 at 08:00 PM.
- 08-27-2010, 01:45 PM #7
No! If you pass each player a field, you are passing a reference to the field. So each player will have a reference to the same field - and will therefore be playing on the same field.If I each player has a variable that stors the field object wouldnt that mean that ever player would be working with a diffrent version od the field? I would need some way updating the current field stats for each player object?
Can you explain a bit more about the field, I havent quite grasped what you want to do. I hesitate to add more until i fully understand :)Java Code:class Field { // code for field } class Player implements Runnable { Field playingOn; public Player (Field aField) { playingOn = aField; } // code for player } class Main { public static final int NUMPLAYERS = 11; public static void main(String[] args) { Field playingField = new Field (); for (int i = 0; i < NUMPLAYERS; i++) { Player temp = new Player (playingField); new Thread(temp).start(); } }
berkeleybross
- 08-27-2010, 05:24 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
Thanks berkeley that exactly what i needed to know...No! If you pass each player a field, you are passing a reference to the field. So each player will have a reference to the same field - and will therefore be playing on the same field.
The filed is just a 2d array that holds player objects. Each player object has a location on the field. When a player "acts()" they can move to any adjacent squar on the field that isnt being occupied.
The way I have it working now is that each player acts in turn. I loop through the list of players and call each players "act()" method one at a time. In the Act() method of each player I pass the field object to them so they can manipulate it like this...
player1.act(field);
The player recieves the current field, makes its move, then the next player does the same thing...
However, I want each player to act independantly and in real time.
to accomplish this I think I can make each player implement runnable and pass them the field object in their constructor (like the example your provided. Thanks).
Then I just have to syncronise the threads so that they dont screw eachother up by acting at the same time.
If you think this sounds legit please let me know..
Thanks
PS: Does the 2d array holding the players have to be declared as static? So I know there is only one instance of the field?Last edited by shuks; 08-27-2010 at 05:32 PM.
- 08-27-2010, 05:59 PM #9
Sounds like the way I'd do it.
If you need help synchronising the thread please ask. Ive got a few ideas how you could do it - but i dont want to give away the game and do it all for you ^^
At this stage you *could* make the 2d array static. However, I think it would be more extendable if you didnt.
What would happen if you played two different matches at once? They would require two different fields with two different arrays of positions.
Also, if you wanted to dispose of one field - the match is over - and make a new one (with different players) You could just reset the 2d array, but i think an even simpler way is to just make a new field object, let java deal with the 2d array.
(The new field would have to be passed to every player, though)
hope that makes sense
berkeleybross
- 08-27-2010, 08:22 PM #10
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
Ya, thanks for your help...
Good point about not making the 2d array static! I wasnt thinking that far a-head.
I will probably need some advice when it comes to syncronizing the threads (I've never done that before). When I get to that stage Maybe I can send you a pm or something.
Thanks Again!Last edited by shuks; 08-27-2010 at 08:24 PM.
Similar Threads
-
Passing Objects
By Java_Developer in forum New To JavaReplies: 6Last Post: 10-29-2009, 01:32 PM -
MVC passing objects
By simo_mon in forum New To JavaReplies: 1Last Post: 07-17-2009, 06:57 AM -
Passing objects into constructors
By aaronfsimons in forum New To JavaReplies: 8Last Post: 04-14-2009, 12:08 PM -
help with passing objects between classes
By aruna1 in forum New To JavaReplies: 7Last Post: 03-22-2009, 02:41 PM -
Passing objects in Java
By jbostjr in forum Advanced JavaReplies: 1Last Post: 10-30-2007, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks