线安全单身,锁
我有一个Singleton课程,这是线程保险箱,我需要锁定它的方法吗?
private static volatile JsonWriter instance;
private static final Object mutex = new Object();
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private JsonWriter() {
}
public static JsonWriter getInstance() {
JsonWriter result = instance;
if (result == null) {
synchronized (mutex) {
result = instance;
if (result == null) {
instance = result = new JsonWriter();
}
}
}
return result;
}
我需要锁定这样的每种方法以确保线程安全吗?
public void write(String filePath, ArrayNode content) throws IOException {
lock.writeLock().lock();
File file = new File(MASTER_DIR + "/" + filePath);
mapper.writerWithDefaultPrettyPrinter().writeValue(Files.newOutputStream(file.toPath()), content);
}
I have a Singleton class which is a thread safe, do I need to lock it methods?
private static volatile JsonWriter instance;
private static final Object mutex = new Object();
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private JsonWriter() {
}
public static JsonWriter getInstance() {
JsonWriter result = instance;
if (result == null) {
synchronized (mutex) {
result = instance;
if (result == null) {
instance = result = new JsonWriter();
}
}
}
return result;
}
do I need to lock each method like this to ensure thread safety?
public void write(String filePath, ArrayNode content) throws IOException {
lock.writeLock().lock();
File file = new File(MASTER_DIR + "/" + filePath);
mapper.writerWithDefaultPrettyPrinter().writeValue(Files.newOutputStream(file.toPath()), content);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最佳性能和线程安全的单身实施是William Pugh Singleton。您不需要同步块和
reentrantreadWritelock
。威廉·普格(William Pugh)的实施可确保多线程安全性和最佳性能,因为它可以避免渴望创造。实际上,只有在嵌套类是由类加载器加载时,即在实际使用嵌套类时,才会创建静态成员实例。在以下实现中,只有在调用
getInstance()
方法时,才能发生这种情况。实际上,相反,该模型实际上允许使用封闭的类而不会引起急切的实例化。这意味着无需初始化实例
而无需安全地使用封闭类提供的任何其他方法;只有getInstance()
方法才会引起它。您的实施可能会像这样:
特别感谢@Holger,他在评论中以重要的澄清和评论做出了贡献。
The best performing and thread-safe Singleton implementation is the William Pugh Singleton. You don't need synchronized blocks and
ReentrantReadWriteLock
.The William Pugh implementation ensures multi-thread safety and the best performances as it avoids eager creation. In fact, the static member INSTANCE, initialized at class level, will be created only when the nested class is loaded by the class loader, i.e. when the nested class will be actually used. In the following implementation, this can only happen when the
getInstance()
method is invoked. In fact, conversely from the EagerSingleton, this model actually allows using the enclosing class without causing an eager instantiation. This means that any other method offered by the enclosing class can be safely used without initializingINSTANCE
; only thegetInstance()
method will cause it.Your implementation could look like this:
A particular thanks to @Holger, who contributed with important clarifications and remarks in the comments.
1
如果要使用同步:
3或可以使用锁:
1 Please dont use synchronized and Lock in one class
2 If you want to use synchronized:
3 Or you can use Locks: