java semaphores,使用java Semaphores解决读者/作者

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's run method is pretty much the same, but we're incrementing writersWorking, also for debugging purposes.

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();

}

}

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.

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
session1 【Objective and Requirement】 Objective: Be familiar with the creation of process and thread on Windows. Requirement: Sub Task 1: Create a console application, "child", which keeps printing out "The child is talking at [system time]" (in a loop, one per 1s). Sub Task 2: Create another console application, "parent". It create a child process to execute “child”. The "parent" process keeps printing out "The parent is talking at [system time]". (one per 1s) Execute "parent" and explain the output you see. Sub Task 3: Create a "winPS" program which can output information about all the running processes. Try to output details about each process, such as PID, executable file name and path, etc. Sub Task 4: In the "mainThread" program, use "CreateThread" to create a child thread. Both the main thread and the child thread keep printing out "[ThreadID] + [System time]". session2 Objective: Create "ps" and "kill" commands on Windows. Requirement: Sub Task 1: On Linux/Unix there are a "ps" command and a "kill" command. The "ps" command can list information about all the running processes. The "kill" command can terminate processes. Based on the tasks finished in Session 1, create "ps" and "kill" commands on Windows. Tips: using "TerminateProcess" to "kill" a process. session3 Objective: Learn how to use semaphore to solve IPC problems. Requirement: Task 3.1. Sleeping barber Use semaphores to solve the problem of sleeping barber. Task 3.2. Reader & Writer Use semaphores to solve the reader and writer problem, with the readers (and writers) have higher priority. session4 Title: Upgrade Linux/Unix commands Problem: Write a program "supershell" that takes another command as an argument and executes that command. For instance, executing: “./supershell cat /usr/greg/readme" would invoke the cat command on the file /usr/greg/readme. After execution of the specified command has completed, "supershell" should display statistics that show some of the system resources the co

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值