Results 1 to 10 of 10
  1. #1
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't pass objects between threads, and this doesn't make sense to me. You pass objects to other objects, same as always.

  3. #3
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    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.

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by berkeleybross View Post
    I agree with fubarable, it should be done in just the same way as normal.

    You could do it in several ways ...

    your approach is to pass the variables to the constructors of the threads so that the variables can be shared during runtime?

  6. #6
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    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:
    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!)
    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?
    Last edited by shuks; 08-26-2010 at 08:00 PM.

  7. #7
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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?
    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.

    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();
            }
        }
    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 :)

    berkeleybross

  8. #8
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    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.
    Thanks berkeley that exactly what i needed to know...

    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.

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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

  10. #10
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    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

  1. Passing Objects
    By Java_Developer in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 01:32 PM
  2. MVC passing objects
    By simo_mon in forum New To Java
    Replies: 1
    Last Post: 07-17-2009, 06:57 AM
  3. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 12:08 PM
  4. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 02:41 PM
  5. Passing objects in Java
    By jbostjr in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2007, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •