老师布置了这样一道题目:
A类的方法B不断地接收传送过来的信息,B方法就不停的把收到的信息放到queue里,每当B方法收到信息时就唤醒一个线程把收到的信息抛给C类的方法.初学者提问请GGJJ多加包含.

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【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
      

  2.   


    难道是我那个queue拼错了?就是放到队列里。然后那个线程从这个队列里取东西作为c类方法的参数。
    我的理解应该是B是一个中转站,他接收信息后传给c类。我想老师是要我们把接收信息和发送信息弄成异步的,而那个队列应该是个缓冲的作用。这样当有方法调用B后当把信息放入队列后可以直接返回了。应该就是这个意思。
      

  3.   

    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();
    }
    }睡前看到 随手写了写 未检查  只是做个参考 有错的话请多多原谅,互相交流才是进步的动力和基础