加快Java深度复制操作 [英] Speeding up java deep copy operations

查看:52
本文介绍了加快Java深度复制操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经使用序列化实现了通用的深度复制机制.

We have implemented a general purpose deep copy mechanism using serialization.

import java.io.*;

public class CopyUtil {

    public static Object clone(Object source) {
        Object retVal = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(source);
            oos.flush();
            oos.close();

            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
            retVal = in.readObject();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return retVal;
    }
}

要维护的对象类相对较多,并且一直在不断发展-这就是我们进行通用克隆机制的原因.我们不喜欢在200多个类上维护 readObject() writeObject()的想法.

There are a relatively large number of object classes, that are evolving all the time, to maintain - This was the reason why we proceeded with a general purpose cloning mechanism. We didn't relish the idea of maintaining readObject() and writeObject() on 200+ classes.

不幸的是,Java中的序列化机制相对较慢,当我们的系统处于峰值负载时,我们遇到了问题.

Unfortunately the serialization mechanism in Java is relatively slow and we are experiencing issues when our system is under peak load.

有没有建议的方法来加快速度,或者(如果我们执行不正确的话)克隆对象的替代方法?

Are there any suggested approaches on how we can speed things up a bit or (in case we have carried this out incorrectly) alternative methods of cloning objects?

推荐答案

Hibernate (特别是在第二级缓存中);我不知道详细信息,但是您可以查看源代码.

A much faster alternative to serialization is implemented in Hibernate (specifically in the 2nd level cache); I don't know the details but you can check out the source code.

您可能会意识到 clone()接口已损坏,因此最好避免使用它,除非有确实令人信服的理由使用它.摘自有效Java第2版,第11项:明智地覆盖克隆

You may be aware that the clone() interface is broken, thus is better avoided, unless there is a really really compelling reason to use it. From Effective Java 2nd Edition, Item 11: Override clone judiciously

鉴于与 Cloneable 相关的所有问题,可以肯定地说其他接口不应该扩展它,并且该类是为继承而设计的(第17项)不应实施它.由于其许多缺点,一些专家程序员只需选择从不覆盖 clone 方法,并且从不调用它,也许复制数组除外.如果您设计一个继承类,请注意,如果您选择不提供行为良好的受保护 Clone 方法,子类将无法实现 Cloneable .

Given all of the problems associated with Cloneable, it’s safe to say that other interfaces should not extend it, and that classes designed for inheritance (Item 17) should not implement it. Because of its many shortcomings, some expert programmers simply choose never to override the clone method and never to invoke it except, perhaps, to copy arrays. If you design a class for inheritance, be aware that if you choose not to provide a well-behaved protected clone method, it will be impossible for subclasses to implement Cloneable.

更新:浅层/深层克隆

来自克隆()API :

创建并返回此对象的副本.复制"的确切含义可能取决于对象的类别.[...]

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. [...]

按照惯例,此方法返回的对象应独立于该对象(将被克隆).为了实现这种独立性,可能有必要在返回super.clone之前修改该对象的一个​​或多个字段.通常,这意味着复制构成要克隆对象的内部深度结构"的任何可变对象,并用对副本的引用替换对这些对象的引用.如果一个类仅包含原始字段或对不可变对象的引用,则通常情况下,无需修改super.clone返回的对象中的任何字段.

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

因此,实际上,约定是进行深层复制.

So in fact, the convention is to do a deep copy.

不过,首选的替代方法是定义一个复制构造函数或一个独立的方法,而不是覆盖 clone().

Still, the preferred alternative is to define a copy constructor or an independent method instead of overriding clone().

这篇关于加快Java深度复制操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆