socket通信过程中,客户端读消息线程加锁
public void setMsg(TransportObject msg) { this.msg=msg; synchronized (this) { notify(); } } public void run() { try { synchronized (this) { wait(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } while(isStart) { //注意,此语句也必须写在while循环中,因为该线程的开启是在client类中开启的,但是setMsg方法 //是在需要时才调用的,即必须让该线程一直运行,一直尝试写,加一个判空的语句来操作具体是否写 if(msg!=null) { try { oos.writeObject(msg); oos.flush(); msg=null;//此语句一定不能去掉,否则oos.writeObject(msg)语句会一直执行 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } try { //Log.i("ClientOutputThread","this print is after while loop"); oos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }