直接操作对象的流(对象的持久化,序列化,可串行性)
ObjectInputStream与ObjectOutputStream成对使用.
被操作的对象,需要实现Serializable (标记接口,通常没有方法)
实现Serializable其就是自动给对象标识一个序列号.
序列号:是,根据类中的成员的数字标识算出来的.不是随机的.
ObjectOutputStream搞出去的对象的序列号必须和相对应类生成的序列号相同才能被识别;
[自定义序列号:]
自己写一个固定的UID如下:
被序列化,意思就是,把堆上所存储的成员转到本地存储.
注意:只能把堆上的成员序列化…在方法区的静态成员是不可以序列化的.
如果非静态被transient修饰的堆上成员也不能被序列化.
管道流(IO流中设计到多线程的对象)可以对接到一起的对象.不用中转站.
类 PipedInputStream
public class PipedInputStream extends InputStream
管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的所有数据字节。
通常,数据由某个线程从 PipedInputStream 对象读取,并由其他线程将其写入到相应的 PipedOutputStream。
不建议对这两个对象尝试使用单个线程,因为这样可能死锁线程。管道输入流包含一个缓冲区,
可在缓冲区限定的范围内将读操作和写操作分离开。如果向连接管道输出流提供数据字节的线程不再存在,
则认为该管道已损坏。
类 PipedOutputStream
public class PipedOutputStream extends OutputStream
可以将管道输出流连接到管道输入流来创建通信管道。管道输出流是管道的发送端。通常,数据由某个线程写入 PipedOutputStream 对象,并由其他线程从连接的 PipedInputStream 读取。不建议对这两个对象尝试使用单个线程,因为这样可能会造成该线程死锁。如果某个线程正从连接的管道输入流中读取数据字节,但该线程不再处于活动状态,则该管道被视为处于 毁坏 状态。
实例:
import java.io.*;
//建立Read读取类,实现Runnable抒写被多线成执行的代码
class Read implements Runnable{
private PipedInputStream in;
//一初始化就有一个管道进来.
Read(PipedInputStream in){
this.in=in;
}
public void run(){
try{
//创建一个byte数组.....
byte[] buf =new byte[1024];
System.out.println("读取前没有数据,阻塞");
int len=in.read(buf);
System.out.println("读到数据,阻塞结束");
//将读取到的buf中的数组变成数组打印出来
String s=new String(buf,0,len);
System.out.println(s);
in.close();
}catch(IOException e){
throw new RuntimeException("管道流读取流失败");
}
}
}
//建立Write写入类,实现Runnable抒写多线程执行的代码.
class Write implements Runnable{
private PipedOutputStream out;
Write(PipedOutputStream out){
this.out=out;
}
public void run(){
try{
System.out.println("开始写入数据,等待6秒后");
Thread.sleep(6000);
//写入数据,参数为byte[],所以,这里用到了String转byte数组的方法
out.write("piped lai la".getBytes());
out.close();
}
catch(Exception e){
throw new RuntimeException("管道输出流失败");
}
}
}
public class Demo{
public static void main(String[] args)throws IOException{
//创立两个管道流
PipedInputStream in =new PipedInputStream();
PipedOutputStream out =new PipedOutputStream();
in.connect(out);//将两个管道对接
//创立对象
Read r=new Read(in);
Write w=new Write(out);
//建立两个线程,并打开调用run中的代码.
new Thread(r).start();
new Thread(w).start();
}
}
Result:
读取前没有数据,阻塞
开始写入数据,等待6秒后
读到数据,阻塞结束
piped lai la