Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 急呀(想做一个多线程同步的类)
neatrain





发贴: 2
积分: 0
于 2005-08-22 17:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请尽量使用准确的文字描述作为标题
Your next post without a proper Subject will be removed.

use [ code ] tag to format source code

Original subject :
急呀!!!请各位高手看看


我想做一个多线程同步的类,具体要求如下:
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);
}

}

}


why edited on 2005-08-22 19:14


话题树型展开
人气 标题 作者 字数 发贴时间
3330 急呀(想做一个多线程同步的类) neatrain 3250 2005-08-22 17:10
2857 Re:急呀(想做一个多线程同步的类) wonderfuture 152 2005-09-01 07:09

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923