D
DiscoStu
Hello everyone,
Im writing an online computer game and Im having some deadlock
trouble on
my servers. I am using multiple threads and I am having some
deadlock trouble with my current design. This design will work for a
short time
but quickly deadlocks at random times, the smaller I make the
Thread.sleep() values the quicker it starts to deadlock so Im thinking
maybe starvation but Im not sure. Here is some pseudocode of what I am
currently doing:
In the main game class there is a Vector containing the list
of all Player objects. This is referenced in the ThreadPoolManager
class below.
Im using vectors as a throwback before I knew they moved onto
ArrayList... but
Vectors are suppose to be thread safe so I stuck with it.
------------------------------------------------------
---- This object contains all the specific information
---- about the player and all of the various services
---- needed to process the users inputs
------------------------------------------------------
public class Player
{
PlayerExecutionThread pet = null;
Socket theSocket;
ObjectInputStream ois;
ObjectOutputStream oos;
public Player() { // Assume all socket stuff above is hooked up }
public void processPlayer() { // Process any messages the user sent to
the server }
public synchronized setPlayerThread(PlayerExecutionThread exe) { pet =
exe; }
public synchronized PlayerExecutionThread getPlayerThread() { return
pet; }
}
----------------------------------------------------------------
---- There are a limited amount of PlayerExecutionThread objects
---- running inside of a thread pool, and created and dished out as
needed
---- I guess this would be considered one of several "consumer"
---- objects. The run method loops and loops and consumes pPlayer
---- objects as they are assigned to this object-thread from
---- the ThreadPoolManager.
----------------------------------------------------------------
public class PlayerExecutionThread implements Runnable
{
Player pPlayer = null;
boolean bRunPlayerThread = true;
public synchronized void setPlayer(Player pEndUser) { pPlayer =
pEndUser; }
public synchronized Player getPlayer() { return pPlayer; }
public run() {
while(bRunPlayerThread == true) {
if(getPlayer() != null) {
if(pPlayer.ois.available() > 0) {
pPlayer.processPlayer();
}
// The processing for this player is finished
pPlayer.setPlayerThread(null);
playerThreadPool.returnObject(this);
pPlayer = null;
}
// Give some sleep time to not hog the thread time
Thread.sleep(500);
}
}
}
---------------------------------------------------------------------
---- This object-thread scans through the list of online players
---- looking for Player objects that dont currently have
PlayerExecutionThread
---- objects attached to them to facilitate processing.
---------------------------------------------------------------------
public class ThreadPoolManager implements Runnable
{
boolean bRunManager = true;
public run() {
while(bRunManager == true) {
synchronized(MainOnlinePlayerList) {
for(int i=0; i<MainOnlinePlayerList.size(); etc) {
Player p = (Player)MainOnlinePlayerList.elementAt(i);
if(p.getPlayerThread() == null) {
// Get an execution thread off the pool
PlayerExecutionThread pet = null;
try { pet = (PlayerExecutionThread)playerThreadPool.borrowObjec(); }
catch(Exception exc) { exc.printStackTrace(System.out); }
// Let the player and the thread know about each other
if(pet != null) {
pet.setPlayer(p);
p.setPlayerThread(pet);
}
}
}
}
// Sleep before the while flips over
Thread.sleep(smallTime);
}
}
Any help would be very appreciated.. I've read about the
wait()/notify()/notifyAll() methods and have tried adding that
functionality but got some errors.... whats the best way to
deadlock-proof my design?
Thanks,
(e-mail address removed)
Im writing an online computer game and Im having some deadlock
trouble on
my servers. I am using multiple threads and I am having some
deadlock trouble with my current design. This design will work for a
short time
but quickly deadlocks at random times, the smaller I make the
Thread.sleep() values the quicker it starts to deadlock so Im thinking
maybe starvation but Im not sure. Here is some pseudocode of what I am
currently doing:
In the main game class there is a Vector containing the list
of all Player objects. This is referenced in the ThreadPoolManager
class below.
Im using vectors as a throwback before I knew they moved onto
ArrayList... but
Vectors are suppose to be thread safe so I stuck with it.
------------------------------------------------------
---- This object contains all the specific information
---- about the player and all of the various services
---- needed to process the users inputs
------------------------------------------------------
public class Player
{
PlayerExecutionThread pet = null;
Socket theSocket;
ObjectInputStream ois;
ObjectOutputStream oos;
public Player() { // Assume all socket stuff above is hooked up }
public void processPlayer() { // Process any messages the user sent to
the server }
public synchronized setPlayerThread(PlayerExecutionThread exe) { pet =
exe; }
public synchronized PlayerExecutionThread getPlayerThread() { return
pet; }
}
----------------------------------------------------------------
---- There are a limited amount of PlayerExecutionThread objects
---- running inside of a thread pool, and created and dished out as
needed
---- I guess this would be considered one of several "consumer"
---- objects. The run method loops and loops and consumes pPlayer
---- objects as they are assigned to this object-thread from
---- the ThreadPoolManager.
----------------------------------------------------------------
public class PlayerExecutionThread implements Runnable
{
Player pPlayer = null;
boolean bRunPlayerThread = true;
public synchronized void setPlayer(Player pEndUser) { pPlayer =
pEndUser; }
public synchronized Player getPlayer() { return pPlayer; }
public run() {
while(bRunPlayerThread == true) {
if(getPlayer() != null) {
if(pPlayer.ois.available() > 0) {
pPlayer.processPlayer();
}
// The processing for this player is finished
pPlayer.setPlayerThread(null);
playerThreadPool.returnObject(this);
pPlayer = null;
}
// Give some sleep time to not hog the thread time
Thread.sleep(500);
}
}
}
---------------------------------------------------------------------
---- This object-thread scans through the list of online players
---- looking for Player objects that dont currently have
PlayerExecutionThread
---- objects attached to them to facilitate processing.
---------------------------------------------------------------------
public class ThreadPoolManager implements Runnable
{
boolean bRunManager = true;
public run() {
while(bRunManager == true) {
synchronized(MainOnlinePlayerList) {
for(int i=0; i<MainOnlinePlayerList.size(); etc) {
Player p = (Player)MainOnlinePlayerList.elementAt(i);
if(p.getPlayerThread() == null) {
// Get an execution thread off the pool
PlayerExecutionThread pet = null;
try { pet = (PlayerExecutionThread)playerThreadPool.borrowObjec(); }
catch(Exception exc) { exc.printStackTrace(System.out); }
// Let the player and the thread know about each other
if(pet != null) {
pet.setPlayer(p);
p.setPlayerThread(pet);
}
}
}
}
// Sleep before the while flips over
Thread.sleep(smallTime);
}
}
Any help would be very appreciated.. I've read about the
wait()/notify()/notifyAll() methods and have tried adding that
functionality but got some errors.... whats the best way to
deadlock-proof my design?
Thanks,
(e-mail address removed)