请教各位 我的线程wait()了 为什么还继续运行???(100分)

hpym365 2006-03-30 07:24:12
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer
{
ServerSocket ssoc=null;
LinkedList<String> rlist=new LinkedList<String>();
LinkedList<String> slist=new LinkedList<String>();
Vector<OutputStream> outs=new Vector<OutputStream>();

public ChatServer() throws Exception
{
ssoc=new ServerSocket(9999);
System.out.println("Server 已经启动!");
while(true)
{
Socket soc=ssoc.accept();
new ServerReceive(soc,rlist,slist).start();
new ServerSend(soc,slist,outs).start();
outs.add(soc.getOutputStream());
System.out.println("一个用户已经连接");
}
}

public static void main(String [] args) throws Exception
{
new ChatServer();
}
}

class ServerReceive extends Thread
{
Socket soc=null;
LinkedList<String> rlist=null;
InputStream in=null;
BufferedReader bin=null;
LinkedList<String> slist=null;

public ServerReceive(Socket soc,LinkedList<String> rlist,LinkedList<String> slist) throws Exception
{
this.soc=soc;
this.rlist=rlist;
in=soc.getInputStream();
this.slist=slist;
bin=new BufferedReader(new InputStreamReader(in));
new Thread(new Runnable()
{
public void run()
{
try
{
read();
System.out.println("*********1");
}
catch(Exception ex){}
}
}).start();
}

public void read() throws Exception
{
String line=null;
while((line=bin.readLine())!=null)
{
rlist.add(line);
}
this.notifyAll();
}

public void run()
{
while(true)
{
String str=this.getMessage();
slist.add(str);
System.out.println("*********2");
}
}
public String getMessage()
{
while(rlist.size()<=0)
{
try
{
wait();
}
catch(Exception ex){}
System.out.println("*********3");
}
return rlist.removeFirst();
}
}

class ServerSend extends Thread
{
Socket soc=null;
OutputStream out=null;
PrintWriter pout=null;
LinkedList<String> slist=null;
Vector<OutputStream> outs=null;

public ServerSend(Socket soc,LinkedList<String> slist,Vector<OutputStream> outs) throws Exception
{
this.soc=soc;
this.outs=outs;
this.slist=slist;
out=soc.getOutputStream();
pout=new PrintWriter(out,true);
}

public void sendMessage(String message)
{
for(OutputStream out:outs)
{
if(out==null)
{
outs.remove(out);
System.out.println("not Message");
}
else
{
new PrintWriter(out,true).println(message);
}
}
}

public String getMessage()
{
while(slist.size()<=0)
{
try
{
wait(); //为什么wait了 现成还在运行???
}
catch(Exception ex){}
System.out.println("*********4");
}
return slist.removeFirst();
}

public void run()
{
while(true)
{
String mes=this.getMessage();
System.out.println("*********5");
this.sendMessage(mes);
}
}
}


在3和4还能运行 大家可以用telnet测试 为什么呀 请各位帮忙 解决马上给分
...全文
124 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hpym365 2006-03-30
  • 打赏
  • 举报
回复
谢谢 给分
shenjialong2008 2006-03-30
  • 打赏
  • 举报
回复
wait()怎么没放在synchronized里面!
interpb 2006-03-30
  • 打赏
  • 举报
回复
帮你看看
别急
hpym365 2006-03-30
  • 打赏
  • 举报
回复
没人帮忙吗????
public String getMessage()
{
while(rlist.size()<=0)
{
try
{
wait();
}
catch(Exception ex){}
System.out.println("*********3");
}
return rlist.removeFirst();
}


public String getMessage()
{
while(slist.size()<=0)
{
try
{
wait(); //为什么wait了 现成还在运行???
}
catch(Exception ex){}
System.out.println("*********4");
}
return slist.removeFirst();
}

大概就是这两个位置的问题 线程wait()了 但是不停止还继续运行

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧