CloneNotSupportedException即使在实现Cloneable时也是如此

Man*_*ish 8 java java-7

为什么以下代码在JDK7中抛出CloneNotSupportedException而在JDK6中抛出?

public class DemoThread extends Thread implements Cloneable {

    /**
     * @param args
     */
    public static void main(String[] args) {
        DemoThread t = new DemoThread();
        t.cloned();
    }

    public DemoThread cloned()
    {
        try {
            return (DemoThread) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

}
Run Code Online (Sandbox Code Playgroud)

bow*_*ore 6

这是Thread clone()在SE 7中的实现

/**
 * Throws CloneNotSupportedException as a Thread can not be meaningfully
 * cloned. Construct a new Thread instead.
 *
 * @throws  CloneNotSupportedException
 *          always
 */
@Override
protected Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
}
Run Code Online (Sandbox Code Playgroud)

线程从未被设计为克隆.做一些阅读引发了我发现这个评论的一个总结得很好:"但我们要么必须禁止克隆或给它明确的语义-后者是不会发生的." - 大卫霍姆斯