Im working on a muli thread server. i have been trying to modify the knovk knock server.
The goal of my project is to
1. collect a player name from 5 clients
2. send the team of 5 players back to each client.
I dont fully understand the new thread to handle each connection. I accept the connection and take a player name fine and i put the thread in a waiting state. i end up with 4 threads in a waiting state a running thread. I use notifyall to awaken my threads but only one continues to run.
I am aware that the 4 other threads are 4 different objects waiting to be notified by there own object but how do i do that?
Can anyone help me?
Team Class
import java.io.*;
import java.util.ArrayList;
public class Team {
final int TEAM_SIZE = 2;
public static int num_members = 0;
public static String player[] = new String[2];
public static String teamMembers;
synchronized String add(String name) {
/*add player name to array*/
player[num_members] = name;
/*increment counter each time player is added team*/
num_members = num_members +1;
if (num_members < TEAM_SIZE)
try {
/*make thread wait*/
wait();
}
catch(Exception e) {}
else
try {
/*awaken threads*/
notifyAll();
/*add all names to one string*/
teamMembers = player[0] + " " + player[1];
}
catch(Exception e) {}
/*return team*/
return teamMembers;
}
} // end add
Protocol Class
import java.net.*;
import java.io.*;
public class KnockKnockProtocol {
private static final int WAITING = 0;
private static final int SENTRESPONSE= 1;
private int state = WAITING;
public String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
theOutput = "Enter Players name : ";
state = SENTRESPONSE;
} else if (state == SENTRESPONSE) {
/*Adding player name to team*/
Team player = new Team();
theOutput = player.add(theInput);
if (Team.num_members == 2){
/*Get all team members*/
theOutput = Team.teamMembers;
}
theOutput = "Player added to team";
state = WAITING;
}
return theOutput;
}
}
Im open to any ideas.
Kevin

New Topic/Question
Reply


MultiQuote

|