-
synchronizing threads
Hi there:
I'm having difficulty trying to understand how I need to make
threads wait() and be reawakened with the notify() method. To
my understanding, according to the literature, both methods should be
surrounded by synchronized methods or bocks and try
catch blocks. For instance:
synchronized(this)
{
try
{
while(threadSuspended)
{
wait();
}
catch(IllegalMonitorStateException
imse){System.out.println("imse in Player.run() wait");}
catch(InterruptedException ie){}
}
}
The best way to explain my dilemma is to show you my code. I have two
classes both of which extend class Thread.
The first is class Game. Class Game then attempts to create two
instances of class Player. In class Game I have an execute()
method that looks like this where you can see how the instances of class
Player are created:
init()
{ ...
Player players = new Player[2];
...
}
synchronized void execute()
{
if(resetting)//this is to ensure this function executes only once
{
if(!starting)
{
if(cc.getThreadSuspended() == false)
cc.setThreadSuspended(true);
}
JBSS.display("Server waiting for players to log on...");
for(int i = 0; i < players.length; i++)
{
try
{
players[i] = new Player(server.accept(), this, JBSS, i);
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}
playerCount ++;
System.out.println(players[i].toString());
players[i].start();
if(i == 0) //Player 1
players[0].setThreadSuspended(true);
if(i == 1) //Player 2
{
try
{
players[0].setThreadSuspended(false);
players[0].notify(); //wakeup Player 1 ...tried
just plain notify() but player 1 still doesn't come out of its wait()
state!
}
catch(IllegalMonitorStateException
imse){System.out.println("imse in game.execute()");}
}
}
}
if(starting)
{
cc.start();
}
else
{
try
{
cc.setThreadSuspended(false);
cc.notify(); //we now have two new players so run
ConnectionCheck every 60 seconds
}
catch(IllegalMonitorStateException
imsex){System.out.println("IMSE in execute() cc.notify()");}
}
playersLoggedOn++;
Player1Resetting = false;
Player2Resetting = false;
}
In class Player, inside the run() method I have the following code
piece:
boolean threadSuspended; //this variable has get and set methods for
it.
if(currentPlayer == "Player1")
{
try
{
output.writeUTF("Waiting for another player!\n" +
"Please wait...\n");
}
catch(IOException ioe){}
if(threadSuspended)
{
synchronized(this)
{
try
{
while(threadSuspended)
{
wait();
}
}
catch(IllegalMonitorStateException
imse){System.out.println("imse in Player.run() wait");}
catch(InterruptedException ie){}
}
}
........
Now, when the execute method runs it creates the players, no problem,
and the first player does go into a wait state like it
should. It is supposed to reawaken when the second player is created.
This is where it gets dicy. If you look at the execute()
method, you'll see that when the second player is created it is supposed
to wake up player 1 with the notify method but player 1
never comes out of its wait() state. The notify method keeps triggering
the IllegalMonitorStateException as you see it written
here. I have changed it from players[0].notify() to simply notify()
just to see what would happen. In this case no exception was
generated but player 1 still didn't wake up from it's wait() state. The
execute method is synchronized and whether or it is I still
get the exception when using players[0].notify(). I don't know how I
can be more specific to the player thead than that! I don't
understand why I can't wake up player 1. How come?
Please help.
Thanks,
Alan
-
Re: synchronizing threads
Here is one of the ways to solve this problem
// Find player 1 thread in your VM thread collection.
Thread threads[] = null;
int numOfThreads = Thread.enumerate(threads);
for(int i = 0; i < numOfThreads; i++){
if( threads[i].getName().equalsIgnoreCase("player1") ){
Player player = (Player)threads[i];
// Call the appropriate methods
// Do not forget to give names to your threads.
Good luck. Hope it helped.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|