📄 userthreadmanger.java
字号:
package chenmin.server;
import java.net.Socket;
import java.util.Vector;
/**
* 类名:用户线程管理类
* 作用:管理用户线程请求
* 文件名:UserThreadManger.java
* 最后修改日期 20060917
* @author Chenmin
*/
public class UserThreadManger implements Runnable
{
private int maxthread;//服务器最大的线程数
Vector thdpool;
public UserThreadManger(int _maxthread)
{
maxthread=_maxthread;
thdpool=new Vector(10,5);
for(int i=0;i<maxthread;i++)
{
Service svc=new Service(i);
Thread svcthd=new Thread(svc);
svcthd.start();
thdpool.addElement(svc);
System.out.println("UserThread "+i+" Started!");
}
}
/**
* 添加一个用户线程
* @param s 用户线程
*/
public void process(Socket s)
{
int i;
for(i=0;i<maxthread;i++)
{
Service currsvc=(Service)thdpool.elementAt(i);
if(!currsvc.isRunning())
{
System.out.println("UserThread "+i+" Notify!");
currsvc.setSocket(s);
currsvc.setRunning(true);
return;
}
}
if(i==thdpool.size())
{
System.out.println("Pool is full,try in another time!");
}
}
public void run()
{
System.out.println("UserThreadManger Started!");
while(true)
{
int i;
for(i=0;i<maxthread;i++)
{
Service currsvc=(Service)thdpool.elementAt(i);
if(currsvc.isRunning()&&currsvc.getFresh())
{
String name=currsvc.getWho();
String word=currsvc.getWord();
if (name.equals("system")||name.equals("System"))
{
if (word.startsWith("name:"))
{
String username=new String(word.substring(5));
currsvc.setUsername(username);
}
else if(word.equals("I")||word.equals("i"))
{
currsvc.sendWord("你的用户名是:"+currsvc.getUsername());
currsvc.setFresh(false);
}
else if(word.equals("List")||word.equals("list"))
{
for(int j=0;j<maxthread;j++)
{
Service tosvc=(Service)thdpool.elementAt(j);
if (tosvc.isRunning())
{
currsvc.sendWord("在线用户:"+tosvc.getUsername());
}
}
currsvc.setFresh(false);
}
break;
}
else{
for(int j=0;j<maxthread;j++)
{
Service tosvc=(Service)thdpool.elementAt(j);
if(tosvc.isRunning()&&tosvc.getUsername().equals(name))
{
String send=currsvc.getUsername()+":"+word;
tosvc.sendWord(send);
System.out.println(send);
currsvc.sendWord("发送成功!");
}
}
currsvc.setFresh(false);
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -