请各位高手帮忙,急!!!!

neatrain 2005-08-22 05:13:36
我想做一个多线程同步的类,具体要求如下:
1.ovclient 类 是一个处理消息的多线程类,负责将收到的消息 通过System.out.println 打印出来。
2.如果没有收到消息,则 ovclient 线程等待5秒钟。如果收到消息,则执行打印操作。
3.消息以同步方式处理,由外部程序产生。
我写的代码如下,但是却得不到预期的效果,请各位大侠帮忙指正。先谢谢了;

ovclient 类代码如下:

import java.util.Vector;
import javax.swing.SwingUtilities;

public class ovclient implements Runnable {

private boolean listening;
private final Vector messages = new Vector();

public ovclient() {
listening = true;
}

public void processNotifyEvent(StringBuffer stringbuffer) {
synchronized (messages) {

messages.add(stringbuffer);

messages.notifyAll();

}
}

public void run() {

if (listening) {

StringBuffer stringbuffer = null;

synchronized (messages) {

while (messages.isEmpty() && listening) {
try {
messages.wait(5000L);
continue;
}
catch (InterruptedException interruptedexception) {
return;
}
catch (Throwable throwable) {
System.out.println("messages 的值为空" + throwable);
}
return;
}

stringbuffer = (StringBuffer) messages.remove(0);

}

final StringBuffer message = stringbuffer;

Runnable runnable = new Runnable() {
public void run() {
System.out.println(
"********************************************************************");
System.out.println(
"********************************************************************");
System.out.println(
"********************************************************************");
System.out.println("应该执行的 message 结果值: " + message.toString());
System.out.println(
"********************************************************************");
System.out.println(
"********************************************************************");
System.out.println(
"********************************************************************");

}
};
try {
SwingUtilities.invokeAndWait(runnable);
}
catch (Exception ex) {
System.out.println("d" + ex.getMessage());
}

}
}

public void quit() {
listening = false;
}

}



测试代码如下:

public class testOVGUIClient {
public testOVGUIClient() {

}

public static void main(String[] args)
{
ovclient serverListener = new ovclient();

Thread serverListenerThread = new Thread(serverListener, "OVGUIClient serverListener");

serverListenerThread.setDaemon(true);
serverListenerThread.start();


for(int i=0;i<10;i++){
StringBuffer testbuffer = new StringBuffer();
testbuffer.append("sssssssssssss ["+ i +"]");
serverListener.processNotifyEvent(testbuffer);
}


}

}



...全文
69 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingfish 2005-08-23
  • 打赏
  • 举报
回复
你的ovclient线程发现有数据取第一条打印完就结束了.
if (listening) ===> while(listening)
neatrain 2005-08-23
  • 打赏
  • 举报
回复
我想要的结果是打印出:
sssssssssssss[0]
sssssssssssss[1]
sssssssssssss[2]
sssssssssssss[3]
sssssssssssss[4]
等值。
但是实际上只打印出了 sssssssssssss[0] 后,线程就像阻塞了一样,后面就没有结果了。
另外,测试代码好像写错了,有点问题,serverListener.processNotifyEvent(testbuffer);不是一次就调用10次,而是由外部线程调用多次。修改后的测试代码如下:


修改后的测试代码:

public class testOVGUIClient {
public testOVGUIClient() {

}

public static void main(String[] args)
{
final ovclient serverListener = new ovclient();

Thread serverListenerThread = new Thread(serverListener, "OVGUIClient serverListener");

serverListenerThread.setDaemon(false);
serverListenerThread.start();

Thread test = new Thread(){
int i=0;
public void run(){

while(true){
try{
StringBuffer testbuffer = new StringBuffer();
testbuffer.append("ssssssssssssssss["+ i +"]");
serverListener.processNotifyEvent(testbuffer);
this.sleep(1000L);
i++;
}catch(Exception ex){
System.out.println("error = " + ex.getMessage());
}
}

}
};
test.start();


}

}

请 高手 再看看。

kingfish 2005-08-22
  • 打赏
  • 举报
回复
你"预期的效果"是什么?
另外测试时setDaemon最好false
从程序看应该打印一个sssssssssssss[0]

62,614

社区成员

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

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