-
Thread problem with static variable - pls help
I have a static vector myUserListVector declared in the threads.
Everytime a user connects to the server, the server will add the username
and ip address to the myUserListVector, and broadcast out to all other
connected users.
If a second user connects to the server, the same thing SHOULD happen.
The problem is like below:
1) userA logs into server->server add to myUserListVector->server broadcast
----------------------------
|myUserListVector|
----------------------------
|userA, IPAddress|
----------------------------
2) userB logs into server->server add to myUserListVector->server broadcast
For userB, he receives what looks like below, which is correct. However,
----------------------------
|myUserListVector|
----------------------------
|userA, IPAddress|
|userB, IPAddress|
----------------------------
For userA, he receives what looks like below. The data should be like userB
above.
----------------------------
|myUserListVector|
----------------------------
It seems to have lost all the elements in the myUserListVector.
What's wrong? I already declared static in myUserListVector, which I expect
that all the threads will have the same copy of the data.
Please help me.
The codes are provided below.
--
Best Regards,
Wing Hoe
---------------------------------------------------------------
Email: winghoe@hotmail.com
ICQ: 2213281
www: http://pwp.maxis.net.my/winghoe
Job Seeking Status : Active
Availability : May 2001 (after degree course finish)
---------------------------------------------------------------
//--------------------------------------------------------------------------
-//
import java.net.*;
import java.io.*;
import java.util.*;
import java.sql.*;
//--------------------------------------------------------------------------
-//
class LoginConnection implements Runnable
{//begin class LoginConnection
//--------------------------------------------------------------------------
-//
private Socket theSocket;
private ObjectOutputStream output;
private ObjectInputStream input;
private Thread connectionThread;
private EventsLog eventsLog;
private static Vector onlineUserVector = new Vector();
private static Vector connections = new Vector();
private String theIPAddress;
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private String SQLString;
private String loginUserName;
private boolean moreRecords;
private String url="jdbc:mysql://127.0.0.1:3306/pc";
private String username="";
private String password="";
private String userID;
private String userPassword;
//--------------------------------------------------------------------------
-//
public LoginConnection(Socket connectionSocket, EventsLog eventsLog)
{//begin LoginConnection()
this.theSocket = connectionSocket;
this.eventsLog = eventsLog;
}//end LoginConnection()
//--------------------------------------------------------------------------
-//
public synchronized void start() throws IOException
{//begin start()
if (connectionThread == null)
{//begin if
try
{//begin try
input = new ObjectInputStream(theSocket.getInputStream());
output = new ObjectOutputStream(theSocket.getOutputStream());
output.flush();
connectionThread = new Thread(this);
connectionThread.start();
}//end try
catch(IOException io)
{//begin catch
eventsLog.writeEventsLog("I/O error occurred in " +
"start() method of LoginConnection...\n" + io);
}//end catch
}//end if
}//end start()
//--------------------------------------------------------------------------
-//
public synchronized void stop()
{//begin stop()
if (connectionThread != null)
{//begin if
eventsLog.writeEventsLog("User: " + loginUserName +
" from IP: " + theSocket.getInetAddress() +
" disconnected from Login Server...");
try
{//begin try
if (connectionThread!= Thread.currentThread())
{//begin if
connectionThread.interrupt();
}//end if
connectionThread=null;
output.close();
input.close();
theSocket.close();
}//end try
catch(IOException io)
{//begin catch
}//end catch
}//end if
}//end stop()
//--------------------------------------------------------------------------
-//
public void run()
{//begin run
try
{//begin try
while (!Thread.interrupted())
{//begin while
String header = input.readUTF();
if(header.equals("LOGIN"))
{//begin if
userID = input.readUTF();
userPassword = input.readUTF();
checkLogin();
}//end if
}//end while
}//end try
catch(Exception ex)
{//begin catch
}//end catch
finally
{//begin finally
System.out.println("connections.removeElement(this)");
connections.removeElement(this);
}//end finally
deleteOnlineUser();
stop();
}//end run
//--------------------------------------------------------------------------
-//
public void checkLogin()
{//begin checkLogin()
openDatabase();
retrieveRecords();
closeDatabase();
}//end checkLogin()
//--------------------------------------------------------------------------
-//
public void openDatabase()
{//begin openDatabase()
try
{//begin try
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
connection = DriverManager.getConnection(url, username, password);
}//end try
catch(ClassNotFoundException cnfex)
{//begin catch
eventsLog.writeEventsLog("Unable to load JDBC:mysql driver...");
}//end catch
catch(SQLException sqlex)
{//begin catch
eventsLog.writeEventsLog("Unable to connect thru SQL...");
}//end catch
catch(InstantiationException ie)
{//begin catch
eventsLog.writeEventsLog("Error creating new instance for " +
"mm.mysql.driver...");
}//end catch
catch(IllegalAccessException iae)
{//begin catch
eventsLog.writeEventsLog("Illegal Access mm.mysql.driver");
}//end catch
}//end openDatabase()
//--------------------------------------------------------------------------
-//
public void retrieveRecords()
{//begin retrieveRecords()
try
{//begin try
SQLString = "SELECT * FROM user WHERE userid='" + userID +
"' AND userpassword='" + userPassword + "'";
statement = connection.createStatement();
resultSet = statement.executeQuery(SQLString);
moreRecords = resultSet.next();
if(!moreRecords)
{//begin if
transmitLoginFailed();
}//end if
else
{//begin else
loginUserName= resultSet.getString("username");
transmitLoginSuccessful(loginUserName);
}//end else
}//end try
catch(SQLException sqlex)
{//begin catch
eventsLog.writeEventsLog("Unable to connect thru SQL...");
}//end catch
}//end retrieveRecords()
//--------------------------------------------------------------------------
-//
public void closeDatabase()
{//begin closeDatabase()
try
{//begin try
resultSet.close();
statement.close();
connection.close();
}//end try
catch(SQLException sqlex)
{//begin catch
eventsLog.writeEventsLog("Unable to connect thru SQL...");
}//end catch
}//end closeDatabase()
//--------------------------------------------------------------------------
-//
public void transmitLoginFailed()
{//begin transmitLoginFailed()
try
{//begin try
output.writeUTF("FAILED");
output.flush();
}//end try
catch(IOException io)
{//begin catch
eventsLog.writeEventsLog("I/O error occurred while " +
"trying to send login status to " +
"client...");
}//end catch
}//end transmitLoginFailed()
//--------------------------------------------------------------------------
-//
public void transmitLoginSuccessful(String loginUserName)
{//begin transmitLoginSuccessful()
try
{//begin try
output.writeUTF("SUCCESSFUL");
output.writeUTF(loginUserName);
output.flush();
eventsLog.writeEventsLog("User : " + loginUserName +
" from IP : " + theSocket.getInetAddress() +
" login successfully...");
System.out.println("connections.addElement(this)");
connections.addElement(this);
addOnlineUser();
}//end try
catch(IOException io)
{//begin catch
eventsLog.writeEventsLog("I/O error occurred while " +
"trying to send login status to " +
" client...");
}//end catch
}//end transmitLoginSuccessful()
//--------------------------------------------------------------------------
-//
public void deleteOnlineUser()
{//begin deleteOnlineUser()
for (int ctr=0; ctr<onlineUserVector.size();ctr++)
{//begin for loop
OnlineUserData currentUser =
(OnlineUserData)onlineUserVector.elementAt(ctr);
if ((currentUser.ipAddress.equals(theIPAddress)) &&
(currentUser.userName.equals(loginUserName)))
{//begin if
System.out.println("removeelementat: " + ctr);
onlineUserVector.removeElementAt(ctr);
System.out.println("onlineUserVector.size(): " +
onlineUserVector.size());
}//end if
}//end for loop
broadcastOnlineUser();
}//end deleteOnlineUser()
//--------------------------------------------------------------------------
-//
public void addOnlineUser()
{//begin addOnlineUser()
theIPAddress = theSocket.getInetAddress().toString();
onlineUserVector.addElement(new OnlineUserData(
theIPAddress, loginUserName));
System.out.println("onlineUserVector.size(): " + onlineUserVector.size());
System.out.println("IPAddress: " + theIPAddress);
System.out.println("username : " + loginUserName);
broadcastOnlineUser();
}//end addOnlineUser()
//--------------------------------------------------------------------------
-//
public void broadcastOnlineUser()
{//begin broadcastOnlineUser()
System.out.println("broadcast to all");
synchronized(connections)
{//begin synchronized
Enumeration items = connections.elements();
int counter=0; //delete this one later ok..test purpose only
while (items.hasMoreElements())
{//begin while
LoginConnection loginConnection =
(LoginConnection) items.nextElement();
counter++;//delete this one later ok...test purpose only
System.out.println("connections no: " + counter);
try
{//begin try
loginConnection.output.writeUTF("USERLIST");
loginConnection.output.writeObject(onlineUserVector);
loginConnection.output.flush();
}//end try
catch(IOException io)
{//begin catch
loginConnection.stop();
}//end catch
}//end while
}//end synchronized
}//end broadcastOnlineUser()
//--------------------------------------------------------------------------
-//
}//end class LoginConnection
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks