java java方法同步与读写互斥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10372668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java method synchronization and read/write mutual exclusion
提问by vijayashankard
I have two methods read()
and write()
as below in a class.
我有两种方法read()
,write()
如下所示。
class Store
{
public void write()
{
// write to store;
}
public string read()
{
// read from store;
}
}
1) The Store
object is a singleton.
1)Store
对象是单例。
2) I have a Writer
class which will write to the store and several Reader
classes which will read from the store at the same time.
2)我有一个Writer
将写入商店的Reader
类和几个将同时从商店读取的类。
My requirement is that when the writer is writing to the store, all the readers should wait. i.e., when control is in write()
, all the calls to read()
should be blocked. How do I achieve this? I have tried synchronize(Store.class)
in the write()
method, but doesn't seem like work for me.
我的要求是,当作者向商店写信时,所有读者都应该等待。即,当控制在 时write()
,所有对 的调用都read()
应该被阻止。我如何实现这一目标?我已经尝试synchronize(Store.class)
过该write()
方法,但似乎对我不起作用。
回答by Tudor
The best option in this case is to use a reader-writer lock: ReadWriteLock. It allows a single writer, but multiple concurrent readers, so it's the most efficient mechanism for this type of scenario.
在这种情况下,最好的选择是使用读写锁:ReadWriteLock。它允许单个写入器,但允许多个并发读取器,因此它是此类场景的最有效机制。
Some sample code:
一些示例代码:
class Store
{
private ReadWriteLock rwlock = new ReentrantReadWriteLock();
public void write()
{
rwlock.writeLock().lock();
try {
write to store;
} finally {
rwlock.writeLock().unlock();
}
}
public String read()
{
rwlock.readLock().lock();
try {
read from store;
} finally {
rwlock.readLock().unlock();
}
}
}
回答by Peter Lawrey
synchronized
is the simplest solution so you should explain what you mean by "doesn't seem like work for me" and perhaps show us how you used it.
synchronized
是最简单的解决方案,因此您应该解释“对我来说似乎不起作用”的意思,并可能向我们展示您如何使用它。
The better solution is to use ReentrantReadWriteLockbecause it allows concurrent access to readers as well.
更好的解决方案是使用ReentrantReadWriteLock,因为它也允许并发访问读者。
回答by JB Nizet
When a method is synchronized on some object, a thread executing this method blocks all the other threads that try to enter another block/method that is synchronized on the same object.
当某个对象上的方法同步时,执行此方法的线程会阻止所有其他线程尝试进入在同一对象上同步的另一个块/方法。
So, if you want the writer threads to forbid any reader thread to read, you must synchronize both the write and the read method. There is no reason to synchronize on the Store class. Synchronizing on the Store object is more natural:
因此,如果您希望写入线程禁止任何读取线程读取,您必须同步写入和读取方法。没有理由在 Store 类上进行同步。在 Store 对象上同步更自然:
public synchronized void write() {
write to store;
}
public synchronized String read() {
read from store;
}
This, however, (maybe) has a drawback: it also forbids two reader threads to read at the same time. If you really need this to happen, you should use a ReadWriteLock. But this will lead to code that is less performant, and harder to understand and maintain. I would only use it if I have measured that this is needed.
然而,这(也许)有一个缺点:它也禁止两个读取器线程同时读取。如果你真的需要这种情况发生,你应该使用ReadWriteLock。但这会导致代码性能下降,更难理解和维护。如果我已经测量到需要这样做,我只会使用它。
回答by Droid Teahouse
Take care to read this article:
请注意阅读这篇文章:
https://blog.takipi.com/java-8-stampedlocks-vs-readwritelocks-and-synchronized/
https://blog.takipi.com/java-8-stampedlocks-vs-readwritelocks-and-synchronized/
- RWLocks can be slow especially when there are many readers to writers
- synchronized is on average the most reliably fast
- there are new locks like StampedLock
- RWLocks 可能很慢,尤其是当有很多读者到作者时
- 同步平均是最可靠的快速
- 有像 StampedLock 这样的新锁
回答by forty-two
Use a ReadWriteLock from java.util.concurrent package
使用 java.util.concurrent 包中的 ReadWriteLock