求教输入输出流解决思路

求教输入输出流classReadWorkThreadextendsThread{publicvoidrun(){try{while(!exitFlag){Stringstrin.re

求教输入输出流
class   ReadWorkThread   extends   Thread   {
                public   void   run()   {
                        try   {
                                while   (!exitFlag)   {
                                        String   str   =   in.readUTF();
                                        if   (str   !=   null)   {
                                                ui.receiveTF.setString(str);
                                        }
                                }
                        }   catch   (IOException   e)   {
                                if   (!exitFlag)
                                        ui.state.setText( "读取数据失败 ");
                        }
                }
        }


class   WriteWorkThread   extends   Thread   {
                DataOutputStream   out   =   null;
                public   WriteWorkThread(DataOutputStream   out){
                        this.out   =   out;
                }
                public   void   run()   {
                        try   {
                                while   (!exitFlag)   {
                                        synchronized   (this)   {
                                                try   {
                                                        wait();
                                                }   catch   (InterruptedException   e)   {
                                                        e.printStackTrace();


                                                }
                                                if   (exitFlag)//   可能因为关闭操作被打断
                                                        break;
                                                if   (sendText   !=   null)
                                                        out.writeUTF(sendText);
                                                        out.flush();
                                        }
                                }
                                out.close();
                        }   catch   (IOException   e)   {
                                if   (!exitFlag)
                                        ui.state.setText( "写数据失败 ");
                        }
                }
        }
这段程序读线程在第一次读取的时候没有任何问题,但是第二次就会发生IOException
"读取数据失败 ",不知道为什么?

[解决办法]
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {


你的代码不全吧,这里在等待,没有看到你notify();或notifyAll()方法