Results 1 to 4 of 4
- 08-10-2009, 01:08 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 7
- Rep Power
- 0
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
- 08-20-2009, 08:42 PM #2
I would store the invitations in a blocking queue:
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.Java Code:import java.util.concurrent.LinkedBlockingQueue;
- 09-28-2009, 08:33 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
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
- 09-28-2009, 09:13 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
code to create a folder in java
By radhika123 in forum New To JavaReplies: 7Last Post: 07-21-2011, 11:21 AM -
Looking for help on drawing stuff in a jPanel
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 08-28-2009, 06:00 PM -
How to create directory through Java Code
By Java Tip in forum java.ioReplies: 1Last Post: 04-14-2009, 03:34 PM -
Simple Stuff 0.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-19-2008, 04:27 PM -
Creating a java gui to create xml code
By Jman in forum New To JavaReplies: 3Last Post: 04-27-2008, 06:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks