Results 1 to 4 of 4
  1. #1
    Mikey_S is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default Trying to create a code for queue, complex stuff...

    Hey folks,
    I'm writing a code for creating a queue to an online checkers game.
    Basically, when a user invites another user to a game, if the recipient is already in the process of receiving invitations from other users, then the sender's invitation will be added to an invitation queue.

    From the recipient side (GUI) - once he receives an invitation and the queue is empty, it'll immediately bring up a dialog asking him if he approves that invitation. Now, in the meantime, while the dialog is open (which means the recipient hasn't approved the invitation yet) the recipient receives another invitation from another user, that invitation will be added to a certain queue, and wait for the results of the first invitation. if the user approves the first invitation, then the 2nd invitation will be rejected automatically and won't show up the expected dialog.

    Each invitation is handled through a different thread, so multiple invitations can be sent simultaneously and calls a synchronized method which is supposed to take care of the dialog appearance, so that no multiple dialogs will open up at once.

    Now the problem is that on the sender's side, there's an option to cancel the invitation request, this sends an object through the output object stream to the recipient that lets him know that the sender's wants to be removed from that list.

    How can I cancel a thread from entering that synchronized method as soon as I get the Cancel request at the recipient's side?

    Thanks,

    Mikey

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    I would store the invitations in a blocking queue:

    Java Code:
    import java.util.concurrent.LinkedBlockingQueue;
    Then have a thread that constantly checks (or every 50-1000ms) to see 1) client is idle, i.e. not playing a game or have a dialog open 2) there are any still valid invites; if so grab the next invite and push it to the method that opens a dialog.

  3. #3
    deschutron is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    so when a dialog is open and another invitation arrives, does your system automatically call the method to create a dialog for the new invitation, which only waits because that method is synchronised?

    If so, then you need to get the new invitations to wait before they try to create a new dialog.
    You might be able to do that with this:
    In the dialog creating thread:
    1. check whether a dialog is open
    2. if a dialog is open
    2.1. set a variable to say this thread is sleeping
    2.2. call Thread.sleep()
    2.3. wait for the sleep to be interrupted
    2.4. set the sleeping variable to say this thread isn't sleeping
    2.4. go back to 1.
    3. if no dialog is open
    4. check if the game is still available (i.e. if the earlier invitation was rejected)
    5. if the game is still available,
    5.1. set a variable to say a dialog is running
    5.2. start the dialog for this invitation

    when the dialog closes, get it to do this:
    1. set the dialog-is-running variable to say no dialog is running
    2. check the sleeping variable
    3. if the dialog-creating thread is sleeping,
    3.1. wake it up with (the dialog-creating thread).interrupt()

    Deschutron

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    That all sounds suspicious. The EDT should be the dialog creating thread and it should never be called sleep on. Better go the route suggested by mrmatt above.

Similar Threads

  1. code to create a folder in java
    By radhika123 in forum New To Java
    Replies: 7
    Last Post: 07-21-2011, 11:21 AM
  2. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 06:00 PM
  3. How to create directory through Java Code
    By Java Tip in forum java.io
    Replies: 1
    Last Post: 04-14-2009, 03:34 PM
  4. Simple Stuff 0.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-19-2008, 04:27 PM
  5. Creating a java gui to create xml code
    By Jman in forum New To Java
    Replies: 3
    Last Post: 04-27-2008, 06:56 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
  •