通過序列化進行深層複製(java)

來源:互聯網
上載者:User

若研究一下第10章介紹的那個Java 1.1對象序列化樣本,可能發現若在一個對象序列化以後再撤消對它的序列化,或者說進行裝配,那麼實際經曆的正是一個“複製”的過程。
那麼為什麼不用序列化進行深層複製呢?下面這個例子通過計算執行時間對比了這兩種方法:
 

//: Compete.javaimport java.io.*;class Thing1 implements Serializable {}class Thing2 implements Serializable {  Thing1 o1 = new Thing1();}class Thing3 implements Cloneable {  public Object clone() {    Object o = null;    try {      o = super.clone();    } catch (CloneNotSupportedException e) {      System.out.println("Thing3 can't clone");    }    return o;  }}class Thing4 implements Cloneable {  Thing3 o3 = new Thing3();  public Object clone() {    Thing4 o = null;    try {      o = (Thing4)super.clone();    } catch (CloneNotSupportedException e) {      System.out.println("Thing4 can't clone");    }    // Clone the field, too:    o.o3 = (Thing3)o3.clone();    return o;  }}public class Compete {  static final int SIZE = 5000;  public static void main(String[] args) {    Thing2[] a = new Thing2[SIZE];    for(int i = 0; i < a.length; i++)      a[i] = new Thing2();    Thing4[] b = new Thing4[SIZE];    for(int i = 0; i < b.length; i++)      b[i] = new Thing4();    try {      long t1 = System.currentTimeMillis();      ByteArrayOutputStream buf =         new ByteArrayOutputStream();      ObjectOutputStream o =        new ObjectOutputStream(buf);      for(int i = 0; i < a.length; i++)        o.writeObject(a[i]);      // Now get copies:      ObjectInputStream in =        new ObjectInputStream(          new ByteArrayInputStream(            buf.toByteArray()));      Thing2[] c = new Thing2[SIZE];      for(int i = 0; i < c.length; i++)        c[i] = (Thing2)in.readObject();      long t2 = System.currentTimeMillis();      System.out.println(        "Duplication via serialization: " +        (t2 - t1) + " Milliseconds");      // Now try cloning:      t1 = System.currentTimeMillis();      Thing4[] d = new Thing4[SIZE];      for(int i = 0; i < d.length; i++)        d[i] = (Thing4)b[i].clone();      t2 = System.currentTimeMillis();      System.out.println(        "Duplication via cloning: " +        (t2 - t1) + " Milliseconds");    } catch(Exception e) {      e.printStackTrace();    }  }} ///:~

其中,Thing2和Thing4包含了成員對象,所以需要進行一些深層複製。一個有趣的地方是儘管Serializable類很容易設定,但在複製它們時卻要做多得多的工作。複製涉及到大量的類設定工作,但實際的對象複製是相當簡單的。結果很好地說明了一切。下面是幾次運行分別得到的結果:
的確
 

Duplication via serialization: 3400 MillisecondsDuplication via cloning: 110 MillisecondsDuplication via serialization: 3410 MillisecondsDuplication via cloning: 110 MillisecondsDuplication via serialization: 3520 MillisecondsDuplication via cloning: 110 Milliseconds

除了序列化和複製之間巨大的時間差異以外,我們也注意到序列化技術的運行結果並不穩定,而複製每一次花費的時間都是相同的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.