Why does the following code throw CloneNotSupportedException in JDK7 but NOT in 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;
}
}
Here’s Thread’s implementation of
clone()in SE 7Threads were never designed to be cloned. Doing some reading sparked off one of the comments I found this summed it up quite well : “But we either
have to disallow cloning or give it meaningful semantics – and the
latter isn’t going to happen.” — David Holmes