问题1:用ObjectOutputStream对象的writeObject写一个对象时,该对象必须new出来,如果只是修改对象的某一成员的话,用ObjectInputStream读出来的还是先前的对象,这是为什么?
问题2:编了一个网络通讯程序,pipedinputstream只用来同步2个线程,一个把接收到的客户端数据对象写入管道,另一个从管道中获得数据对象发向客户端,注:管道和socket没有关系,只是同步服务器的两个读写线程读写数据对象,当任何一个客户端关闭后(因为同步流,socket会抛异常),pipedinputstream的读方法(用ObjectInputStream包装过了)会抛Piped Broken异常,不明白为什么会抛异常。
由于代码太多,编了个测试代码贴上来,不过也够多了。希望高手赐教。
//用来序列化的对象
import java.io.Serializable;public class BasicPacket implements Serializable { private static final long serialVersionUID = 1L; public boolean close; public String time; public String ip; public BasicPacket(boolean close) {
this.close = close;
}}//主程序
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;public class MyTest {
public static void main(String[] args) {
new MyTest();
} public MyTest() {
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = null;
ObjectOutputStream oos = null; try { pis = new PipedInputStream(pos);
oos = new ObjectOutputStream(pos); Thread1 t1 = new Thread1(pis); Thread2 t2 = new Thread2(oos);

t1.start();
t2.start();

System.out.println("thread start");

Thread.sleep(5000);
                        //关闭正在sleep的线程以抛出piped broken异常,实际程序中和这里不一样,但抛出的异常相同
                           //不明白线程异常为何会导致piped broken异常?
t2.interrupt();
t2.end();
t1.join();
t2.join();
System.out.println("thread end");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (pis != null) {
try {
pis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} // 读
class Thread1 extends Thread {
private PipedInputStream pis = null;

private boolean flag = false; public void end() {
flag = false;
} public Thread1(PipedInputStream pis) throws Exception {
this.pis = pis;
} public void run() {
flag = true;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(pis);
while (flag) { Object obj = (Object)ois.readObject();
if(obj instanceof BasicPacket){
BasicPacket packet = (BasicPacket)obj;
                                                //读出的对象
System.out.println("read:" + packet.ip);
}

}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
ois = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
} //写
class Thread2 extends Thread {
private ObjectOutputStream oos = null;


private boolean flag = false; public void end() {
flag = false;
} public Thread2(ObjectOutputStream oos) throws Exception {
this.oos = oos;
} public void run() {
flag = true;
int i = 0;


try {
                                //放这里每次都是第一次的数据,packet.ip 这个赋值没用
                                BasicPacket packet = new BasicPacket(false);
while (flag) {
                                        //只有每次new一个对象,读出来的才是正确的,这是为什么?         //BasicPacket packet = new BasicPacket(false);
i = i + 1 % 100;
packet.ip = String.valueOf(i);;

synchronized (oos) {
oos.writeObject(packet);
oos.flush();
}
System.out.println("send:" + packet.ip);
                                        //为了能睡觉时被打断,会抛异常,这个应该众所周知吧,java核心卷里写着
sleep(2000);
}
} catch (Exception e) {
e.printStackTrace();
} }
}


}