java线程问题 老师布置了这样一道题目:A类的方法B不断地接收传送过来的信息,B方法就不停的把收到的信息放到queue里,每当B方法收到信息时就唤醒一个线程把收到的信息抛给C类的方法.初学者提问请GGJJ多加包含. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主【infinitejustice】截止到2008-07-01 22:17:51的历史汇总数据(不包括此帖):发帖的总数量:0 发帖的总分数:0 结贴的总数量:0 结贴的总分数:0 无满意结贴数:0 无满意结贴分:0 未结的帖子数:0 未结的总分数:0 结贴的百分比:---------------------结分的百分比:---------------------无满意结贴率:---------------------无满意结分率:---------------------如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html 难道是我那个queue拼错了?就是放到队列里。然后那个线程从这个队列里取东西作为c类方法的参数。我的理解应该是B是一个中转站,他接收信息后传给c类。我想老师是要我们把接收信息和发送信息弄成异步的,而那个队列应该是个缓冲的作用。这样当有方法调用B后当把信息放入队列后可以直接返回了。应该就是这个意思。 class ThreadCommunicate { class A implements Runnable{ private boolean isMessage; private LinkedList list; public void run(){ synchronized(this){ while(isMessage){ try{ this.wait(); }catch(Exception){}; } } synchronized(this){ while(System.in != null){ list.add(System.in); } isMessag = true; this.notify(); } } } class C extends Thread{ public A thesend; public C(A send){ this.thesend = send; } public void run(){ synchronized(thesend){ while( thesend.list.size[] != 0){ thesend.wait(); } } receiveMessage(thesend.list); //Message synchronized(thesend){ thesend.isMessage = false; thesend.notify(); } } void receiveMessage(LinkedList thesend); } public static void main(String args[]){ A sendMessage = new A(); C receiveMessage = new C(sendMessage); Thread thread1 = new Thread(sendMessage); Thread thread2 = new Thread(receiveMessage); thread1.start(); thread2.start(); }}睡前看到 随手写了写 未检查 只是做个参考 有错的话请多多原谅,互相交流才是进步的动力和基础 如何加强Java 基础? Java如何改变图片中图像的内容 自定义类里数组的赋值问题 JScrollPane如何设置垂直滚动条自动滚动到最下方?急! JTable添加列后原先显示的外观属性丢失? 多线程读写的问题 新手上路:关于列表框的(中文)值如何取? 哪位大侠给看看这句话的意思?(多态调用递归) J2SE 5.0 和 JDK 1.5 有啥区别? 问题3 - 字符串数组的类名? 对运行Java所需要的文件的问题 谁能帮我做个作业?可能是考试题 !!谢谢啦。。
楼主【infinitejustice】截止到2008-07-01 22:17:51的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
难道是我那个queue拼错了?就是放到队列里。然后那个线程从这个队列里取东西作为c类方法的参数。
我的理解应该是B是一个中转站,他接收信息后传给c类。我想老师是要我们把接收信息和发送信息弄成异步的,而那个队列应该是个缓冲的作用。这样当有方法调用B后当把信息放入队列后可以直接返回了。应该就是这个意思。
class A implements Runnable{
private boolean isMessage;
private LinkedList list;
public void run(){
synchronized(this){
while(isMessage){
try{
this.wait();
}catch(Exception){};
}
}
synchronized(this){
while(System.in != null){
list.add(System.in);
}
isMessag = true;
this.notify();
}
}
}
class C extends Thread{
public A thesend;
public C(A send){
this.thesend = send;
}
public void run(){
synchronized(thesend){
while( thesend.list.size[] != 0){
thesend.wait();
}
}
receiveMessage(thesend.list); //Message
synchronized(thesend){
thesend.isMessage = false;
thesend.notify();
}
}
void receiveMessage(LinkedList thesend);
}
public static void main(String args[]){
A sendMessage = new A();
C receiveMessage = new C(sendMessage);
Thread thread1 = new Thread(sendMessage);
Thread thread2 = new Thread(receiveMessage);
thread1.start();
thread2.start();
}
}睡前看到 随手写了写 未检查 只是做个参考 有错的话请多多原谅,互相交流才是进步的动力和基础