我用hashmap存放通信线程//其中hm的key为用户名,value为对应的通信线程,hashmap定义在ManagerClientSocket中
public class ManagerClientSocket {
//用hashmap来存放各个客户端与服务器的连接socket,并用客户名标识这些socket
public static HashMap<String,SerConClientThread> hm=new HashMap<String,SerConClientThread>();
//存存放线程
public static void addClientSocket(String uid,SerConClientThread sc){
hm.put(uid, sc);
}
//取得线程
public static SerConClientThread getClientSocket(String uid){
return (SerConClientThread)hm.get(uid);
}
}
然后我在服务器类Server中存进线程,代码如下://SerConClientThread是一个线程类,用于实现客户端与服务器的连接
SerConClientThread scct=new SerConClientThread(s);
ManagerClientSocket.addClientSocket(u.getUsername(), scct);最后我在SerConClientThread中去hm中取值,却总是取得null,代码如下:SerConClientThread sc=ManagerClientSocket.getClientSocket(u.getUsername()); //取线程请教网友,这是哪里出了问题?
public class ManagerClientSocket {
//用hashmap来存放各个客户端与服务器的连接socket,并用客户名标识这些socket
public static HashMap<String,SerConClientThread> hm=new HashMap<String,SerConClientThread>();
//存存放线程
public static void addClientSocket(String uid,SerConClientThread sc){
hm.put(uid, sc);
}
//取得线程
public static SerConClientThread getClientSocket(String uid){
return (SerConClientThread)hm.get(uid);
}
}
然后我在服务器类Server中存进线程,代码如下://SerConClientThread是一个线程类,用于实现客户端与服务器的连接
SerConClientThread scct=new SerConClientThread(s);
ManagerClientSocket.addClientSocket(u.getUsername(), scct);最后我在SerConClientThread中去hm中取值,却总是取得null,代码如下:SerConClientThread sc=ManagerClientSocket.getClientSocket(u.getUsername()); //取线程请教网友,这是哪里出了问题?
for(Object o:set)
{
String key=(String)o;//获得键
String value=mpa.get(key);//通过键取得值
}
不晓得你做的那个可以根据这个思路做不? 我也只是随便看下你的代码 没深入分析 也是初学者嘛
//用hashmap来存放各个客户端与服务器的连接socket,并用客户名标识这些socket
private HashMap<String,SerConClientThread> hm=new HashMap<String,SerConClientThread>();
//存存放线程
public void addClientSocket(String uid,SerConClientThread sc){
hm.put(uid, sc);
}
//取得线程
public SerConClientThread SerConClientThread getClientSocket(String uid){
return hm.get(uid);
}
}然后嗲用的时候用new
SerConClientThread scct=new SerConClientThread(s);
ManagerClientSocket m = new ManagerClientSocket()
m.addClientSocket(u.getUsername(), scct);SerConClientThread sc=m.getClientSocket(u.getUsername()); //取线程