我想做一个多线程同步的类,具体要求如下:
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);
}
}}
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);
}
}}
另外测试时setDaemon最好false
从程序看应该打印一个sssssssssssss[0]
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();
}}请 高手 再看看。
if (listening) ===> while(listening)