我在面试中被问到这些。
Do we need to take care of clone method in a concurrent environment ? Can we synchronize the clone method ?
Does it make any sense to use clone method in singleton classes ?
面试时我没有给出令人信服的答案。
最佳答案
可能 99.99% 的情况下是的,但您只需要像类的任何其他方法一样考虑克隆,并在必要时根据您的特定上下文同步它。同步一个在其 Base 类中未同步的方法并没有错。另一方面,在覆盖同步方法时不同步方法可能是一个错误,即使代码编译正常并且没有发出警告...
只需简单介绍一下单例设计模式,一个令人信服的答案可能就是“否”。
关于Java :Questions on clone method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9204650/