为什么以下代码在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)
这是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)
线程从未被设计为克隆.做一些阅读引发了我发现这个评论的一个总结得很好:"但我们要么必须禁止克隆或给它明确的语义-后者是不会发生的." - 大卫霍姆斯
| 归档时间: |
|
| 查看次数: |
2654 次 |
| 最近记录: |