Results 1 to 6 of 6
  1. #1
    Axephilic is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Best way to wait for a message

    Hey guys,

    Quick question hopefully...

    I have a 2 package server, 1 is for the sever and chat, the other runs card games, server-side. As it goes through the game code, it receives messages from a socket on the chat server. How can I have my game code wait for a message at a certain point, then once it gets the message continue? Example, the code asks the user what they want to bid, then waits for the client applet to respond.

    Right now I have the code in a while(User.getNextMessage() == null) loop but this doesn't seem to be working or a good practice. I have the chat server receive the message and add it to an arraylist in User. THen the getNextMessage function returns messages[0].

    Any help is appreciated.

    Thank you,
    Adam

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Best way to wait for a message

    If you are sending a message and want to wait for a response, many of the read methods will block waiting for the response.
    Another way is to use the Object class's wait and notify methods.

    There are some more modern java classes that will do what you want. I'll have to research them or someone that knows will post a reference here for you.

  3. #3
    Axephilic is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Best way to wait for a message

    Thank you Norm.

    The wait and notify wasn't working for me because I believe it has to be in a thread from my research. The User object is not threaded, nor are any of the game objects.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Best way to wait for a message

    Objects are not "on" threads.
    The execution of code in methods is done on a thread. All code executes on a thread.
    If you want to do several things at the same time, it is useful to use threads. For example one thread could wait for input from another PC while another thread handles the GUI for the user.

    I think the java.util.concurrent package has several useful classes for your needs. I haven't used many of them in a long time. Your could do a Search here for code samples. Or google.
    Last edited by Norm; 12-06-2011 at 04:28 PM.

  5. #5
    Axephilic is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Best way to wait for a message

    Am I overthinking this or could I just simple split the game methods?

    I appreciate your help but from the java docs and threads I couldn't find anything that had the right fit.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Best way to wait for a message

    Try doing some design work by marking a list of the events in the order that they will occur.
    Make parallel columns with a column for each source of events.
    What execution steps in one column will have to wait for an event in another column before it can continue?

Similar Threads

  1. Wait() and Notify()
    By SiX in forum New To Java
    Replies: 15
    Last Post: 07-28-2011, 04:29 PM
  2. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 04:24 PM
  3. Replies: 6
    Last Post: 06-04-2010, 08:43 PM
  4. about wait() and notifyALL
    By denis in forum Threads and Synchronization
    Replies: 13
    Last Post: 04-22-2009, 08:28 AM
  5. How to use sleep() to wait for a while
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:32 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
  •