public boolean connectedOnGameServer = false;
public final Object conGameServerMonitor = new Object();
public void connectedToGameServer() {
synchronized (conGameServerMonitor) {
if (connectedOnGameServer != false)
throw new RuntimeException("Player connected twice");
connectedOnGameServer = true;
conGameServerMonitor.notifyAll();
}
}
public void waitForGameServerConnection() {
synchronized (conGameServerMonitor) {
try {
long startTime = System.currentTimeMillis();
long waited = 0;
while (!connectedOnGameServer && waited < GAMESERVER_CONNECT_TIMEOUT) {
conGameServerMonitor.wait(GAMESERVER_CONNECT_TIMEOUT - waited);
waited = System.currentTimeMillis() - startTime;
}
if (waited > GAMESERVER_CONNECT_TIMEOUT && connectedOnGameServer) {
throw new RuntimeException("Client didn't connect to game server in time (" + GAMESERVER_CONNECT_TIMEOUT + " ms)");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for client to connect to game server", e);
}
}
}
What I need is:
- Thread A calls
waitForGameServerConnection - Thread B calls
connectedToGameServer - Thread A continues