多个读者和多个作者(我是说多个)同步 [英] Multiple readers and multiple writers(i mean multiple) synchronization

查看:59
本文介绍了多个读者和多个作者(我是说多个)同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一项功能,该功能需要一种读/写锁的变体,该变体可以允许并发多个写程序.

I am developing a feature that needs a variant of read/write lock that can allow concurrent multiple writers.

标准读/写锁允许多个读取器或单个写入器同时运行.我需要一个可以同时允许多个读者或多个作者的变体.因此,它绝对不能同时允许读者和作家.但是,可以允许同时有多个作者或同时有多个读者.

Standard read/write lock allows either multiple readers or single writer to run concurrently. I need a variant that can allow multiple readers or multiple writers concurrently. So, it should never allow a reader and a writer concurrently. But, its okay to allow multiple writers at the same time or multiple readers at the same time.

我希望我很清楚.到目前为止,我找不到任何现有算法.我可以考虑使用一些队列等方法来完成此操作的方法.但是,我不希望自己冒险,除非不存在.

I hope I was clear. I couldn't find any existing algorithm so far. I can think of couple of ways to do this using some queues and etc. But, I dont want to take a risk of doing it myself unless none exists.

你们知道任何现有的计划吗?

Do you guys know of any existing scheme?

谢谢

推荐答案

您正在寻找的概念是可重入锁.您需要能够尝试获取该锁,并且如果该锁已经被使用(被称为可重入锁),则不会被阻塞.Java中有一个可重入锁的本地实现,因此我将在Java中说明此示例.( http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html ).

The concept you are looking for is a Reentrant lock. You need to be able to try to acquire the lock and not get blocked if the lock is already taken (this is known as reentrant lock). There is a native implementation of a reentrant lock in java so I will illustrate this example in Java. (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html).

因为使用tryLock()时,如果锁不可用,您不会被阻止,您的编写者/阅读者可以继续进行操作.但是,只有在确定没有人可以再进行读/写操作时,才想释放该锁,因此您将需要保留读写器的数量.您将需要同步此计数器,或者使用允许原子递增/递减的本机atomicInteger.在此示例中,我使用了原子整数.

Because when using tryLock() you don't get blocked if the lock is not available your writer/reader can proceed. However, you only want to release the lock when you're sure that no one is reading/writing anymore, so you will need to keep the count of readers and writers. You will either need to synchronize this counter or use a native atomicInteger that allows atomic increment/decrement. For this example I used atomic integer.

Class ReadAndWrite {
 private ReentrantLock readLock;
 private ReentrantLock writeLock;
 private AtomicInteger readers;
 private AtomicInteger writers;
 private File file;

 public void write() {
   if (!writeLock.isLocked()) {
    readLock.tryLock();
    writers.incrementAndGet(); // Increment the number of current writers
    // ***** Write your stuff *****
    writers.decrementAndGet(); // Decrement the number of current writers
    if (readLock.isHeldByCurrentThread()) {
     while(writers != 0); // Wait until all writers are finished to release the lock
     readLock.unlock();
    }
   } else {
     writeLock.lock();
     write();
   }
  }

 public void read() {
   if (!readLock.isLocked()) {
    writeLock.tryLock();
    readers.incrementAndGet(); 
    // ***** read your stuff *****
    readers.decrementAndGet(); // Decrement the number of current read
    if (writeLock.isHeldByCurrentThread()) {
     while(readers != 0); // Wait until all writers are finished to release the lock
     writeLock.unlock();
    }
   } else {
     readLock.lock();
     read();
   }
  }

这里发生的事情:首先,您检查一下锁是否已锁定,以了解是否可以执行将要执行的操作.如果已锁定,则意味着您无法读写,因此您可以使用锁定将自己置于等待状态,并在再次释放锁定时重新调用相同的操作.

What's happening here: First you check if your lock is locked to know if you can perform the action you're going to perform. If it's locked it means you can't read or write so you use lock to put yourself in wait state and re-call the same action when the lock is freed again.

如果未锁定,则使用tryLock锁定另一个操作(如果要读取,则锁定写操作,反之亦然).tryLock不会被锁定,如果它已经被锁定,那么几个写程序可以同时写,而几个读程序可以同时读.当执行与您相同的操作的线程数达到0时,意味着首先拥有锁的人现在可以释放它.此解决方案的唯一不便之处在于,持有锁的线程将必须保持活动状态,直到每个人都可以释放它为止.

If it's not locked, then you lock the other action (if you're going to read you lock writes and vice-versa) using tryLock. tryLock doesn't block if it's already locked, so several writers can write at the same time and several readers can read at the same time. When the number of threads doing the same thing as you reaches 0 it means that whoever held the lock in the first place can now release it. The only inconvenience with this solution is that the thread that holds the lock will have to stay alive until everyone is finished to be able to release it.

这篇关于多个读者和多个作者(我是说多个)同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆