我想做一个多线程同步的类,具体要求如下:
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);
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货