使用java Semaphores解决读者/作者 [英] Solving Readers/Writers using java Semaphores

查看:128
本文介绍了使用java Semaphores解决读者/作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是一个经典的并发问题,我们(我和我的同事)面对这里。我们不懒,我们带来了一些相关的代码,以帮助我们正常。
我们有两个类定义读者和写者,他们都扩展 Thread类,当然覆盖运行方法如下:

So, it's a classical concurrency problem we're (Me and my colleague) facing here. We weren't lazy, We brought some relevant code in order for you to help us properly. We have two classes defining Readers and Writers, they both extend Thread class, and of course override the run method like so:

while(!isInterrupted()) {
 try{ 
     Thread.sleep(for some time)
 }catch(InterruptedException e) {}

 database.readLock();
  readersWorking++; //for debugging purposes
 database.readUnlock();
}

Writer的运行方法几乎相同,但是我们增加了writersWorking,也用于调试目的。

Writer's run method is pretty much the same, but we're incrementing writersWorking, also for debugging purposes.

在我们的主要方法中,我们创建了20个读者和2个作家。他们都通过构造函数注入获得一个Database类的实例。这是数据库:

In our main method we're creating 20 readers and 2 writers. They both get one instance of Database class via constructor injecting. Here is the Database:

class Database {
    Semaphore writeMut = new Semaphore(1);
    Semaphore readMut = new Semaphore(1);
    private int readersWorking = 0;

    public Database() {

    }

    public void readLock() {
        readMut.acquireUninterruptibly();

        if(readersWorking==0) //Am I the first one?
            writeMut.acquireUninterruptibly();
        readersWorking++;

        readMut.release();
    }

    public void writeLock() {
        writeMut.acquireUninterruptibly();
    }

    public void readUnlock() {
        readMut.acquireUninterruptibly();
        readersWorking--;
        if(readersWorking==0) //Am I the last one?
            writeMut.release();
        readMut.release();
    }

    public void writeUnlock() {
        writeMut.release();
    }
}

问题:为什么这个代码会导致我们的读者访问数据库,而作者仍在其中,反之亦然?我们怎么能否认这一点呢?这里的逻辑有什么问题?我们还在寻找一本关于Java并发的好书,如果有人知道这一点。

The question: why does this code result in our Readers accessing the database while the Writers are still in it and vice versa? How can we deny this from happening? What is wrong with our logic here? We're also looking for a good book on concurrency in Java, if anyone knows such.

只要提供的代码不够,这里是完整的代码: http://codepad.org/IJ7e145C

Just in case the code provided isn't enough, here's the full code: http://codepad.org/IJ7e145C

推荐答案

我实际上不是在心情彻底分析你的代码(懒惰,我知道:p),但它听起来像java.util.concurrent.locks包有很多你需要什么。现在除非你坚持使用Java 1.4我强烈建议依靠Java并发工具为你做这个讨厌的工作。

I'm not actually in the mood to thoroughly analyze your code (lazy, I know :p), but it sounds like the java.util.concurrent.locks package has pretty much exactly what you need. Now unless you're stuck with Java 1.4 I'd strongly suggest relying on the Java concurrency utils to do this nasty work for you. You'll make it easier on yourself.

对于这本书,它看起来像这将适合帐单

As for the book, it looks like this will fit the bill.

这篇关于使用java Semaphores解决读者/作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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