Concurrent issue:about read write lock

reentrantlock 2013-12-30 10:08:01
For the following testing code,how to make the read thread can automatically be notified when the write thread finishes writing new data to the container.
In order to improve the performance of concurrent reading data, read lock and write lock are used in the programme,beacause of this,I feel difficult to implement both of threads communicate with each other.

Anybody can provide a solution to reach the objective? Please note the pre-condition: don't use another lock to replace my read-write lock,if so,concurrent performance will be reduced.That's not what I need.
Thanks a lot in advance.

------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class ReadWriteLockTest {

private ArrayList<String> container = new ArrayList<String>();

private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

private ReadLock readLock = readWriteLock.readLock();
private WriteLock writeLock = readWriteLock.writeLock();

public void execute(){
for(int k=0;k<1;k++){
Thread writeThread = new Thread(new Runnable(){

@Override
public void run() {

while(true){
writeLock.lock();
try{
for(int j = 0;j<3;j++){
container.add("_"+j);
}

System.out.println(Thread.currentThread().getName()+" write size:"+container.size());
}
finally{
writeLock.unlock();
}

try {
System.out.println(Thread.currentThread().getName()+" sleeping...");
TimeUnit.SECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


});

writeThread.start();
}

for(int x=0;x<2;x++){
Thread readThread = new Thread(new Runnable(){

@Override
public void run() {

while(true){
readLock.lock();
try{
TimeUnit.SECONDS.sleep(1);
while(container.size()>0){
String str = container.get(0);
synchronized(container){ // remove operation need exclusive lock when read-read
container.remove(str);
}
System.out.println(Thread.currentThread().getName()+" read data:"+str+ " size:"+ container.size());
}


} catch (Exception e){
e.printStackTrace();
}finally{
readLock.unlock();
}

try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


});

readThread.start();
}

}


/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws Exception {
ReadWriteLockTest test = new ReadWriteLockTest();
test.execute();
TimeUnit.SECONDS.sleep(100000);
}

}
...全文
365 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
reentrantlock 2014-01-07
  • 打赏
  • 举报
回复
@Lsheep, please modify my source code if you are confident of your technology. Thanks a million in advance.
reentrantlock 2014-01-07
  • 打赏
  • 举报
回复
I feel no difficulty to implement it if I use ReentrantLock or traditional synchroniz ation mechanism instead of ReentrantReadWriteLock.
reentrantlock 2014-01-07
  • 打赏
  • 举报
回复
引用 11 楼 Lsheep 的回复:
[quote=引用 10 楼 u013192985 的回复:] [quote=引用 9 楼 Lsheep 的回复:] [quote=引用 8 楼 u013192985 的回复:] [quote=引用 5 楼 Lsheep 的回复:] 首先,下次贴代码请放在标签里,就如我下面展示代码那样; 然后,你下面这块代码是有问题的:有可能某个reader执行synchronized代码之前被调度,另一个reader也进入循环得到同一个元素,最后这个元素被remove了两次,好在remove不存在的元素不会抛异常,只会返回false。

while(container.size()>0){
    String str = container.get(0);
    synchronized(container){ // remove operation need exclusive lock when read-read
        container.remove(str);
    }
最后,我不明白你想干什么,当一个writer在写数据时,其他的reader会被阻塞,当writer执行完后,自动会去唤醒一个reader。你为什么想让writer去主动的notify reader呢?就你给的例子而言,代码中有一些sleep方法,如果你想notify一个sleep线程,那只能interrupte了,所以你到底想干什么呢?
当writer执行完后,自动会去唤醒一个reader? Are you sure? Please note,there is not any wait statement in the run method of read thread,how can the write thread awake the read thread? One word,no "wait",how to wake it? Anyway,thanks for your comments.[/quote] 假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。 如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。[/quote] I feel difficult to communicate with you for this issue. I am not sure whether you don't understand my question or lack of knowledge of technology. However, if you really can help me solve this issue,can you paste your code here?[/quote] 好吧,那我们重新捋一下:你想问的是不是下面两个问题之一,或者都是 1,如何使用notify 2,reader和writer是如何通信的[/quote] Yeah,but the pre-condition is you must use ReentrantReadWriteLock to implement.
Lsheep 2014-01-07
  • 打赏
  • 举报
回复
引用 10 楼 u013192985 的回复:
[quote=引用 9 楼 Lsheep 的回复:] [quote=引用 8 楼 u013192985 的回复:] [quote=引用 5 楼 Lsheep 的回复:] 首先,下次贴代码请放在标签里,就如我下面展示代码那样; 然后,你下面这块代码是有问题的:有可能某个reader执行synchronized代码之前被调度,另一个reader也进入循环得到同一个元素,最后这个元素被remove了两次,好在remove不存在的元素不会抛异常,只会返回false。

while(container.size()>0){
    String str = container.get(0);
    synchronized(container){ // remove operation need exclusive lock when read-read
        container.remove(str);
    }
最后,我不明白你想干什么,当一个writer在写数据时,其他的reader会被阻塞,当writer执行完后,自动会去唤醒一个reader。你为什么想让writer去主动的notify reader呢?就你给的例子而言,代码中有一些sleep方法,如果你想notify一个sleep线程,那只能interrupte了,所以你到底想干什么呢?
当writer执行完后,自动会去唤醒一个reader? Are you sure? Please note,there is not any wait statement in the run method of read thread,how can the write thread awake the read thread? One word,no "wait",how to wake it? Anyway,thanks for your comments.[/quote] 假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。 如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。[/quote] I feel difficult to communicate with you for this issue. I am not sure whether you don't understand my question or lack of knowledge of technology. However, if you really can help me solve this issue,can you paste your code here?[/quote] 好吧,那我们重新捋一下:你想问的是不是下面两个问题之一,或者都是 1,如何使用notify 2,reader和writer是如何通信的
Lsheep 2014-01-07
  • 打赏
  • 举报
回复
可能我还是没明白你的意思,下面这段话: how to make the read thread can automatically be notified when the write thread finishes writing new data to the container. 当写进程完成写操作后,如何自动唤醒读进程。 不对呀,在我的理解里,这个本来就是啊,writer在写时,所有的reader都会阻塞,当这个writer释放写锁后,并且没有其他的writer在等待锁时,reader自然就被调度了。下面这段是从java文档里找的。 <p>A thread that tries to acquire a fair read lock (non-reentrantly) * will block if either the write lock is held, or there is a waiting * writer thread. The thread will not acquire the read lock until * after the oldest currently waiting writer thread has acquired and * released the write lock. Of course, if a waiting writer abandons * its wait, leaving one or more reader threads as the longest waiters * in the queue with the write lock free, then those readers will be * assigned the read lock. * * <p>A thread that tries to acquire a fair write lock (non-reentrantly) * will block unless both the read lock and write lock are free (which * implies there are no waiting threads). (Note that the non-blocking * {@link ReadLock#tryLock()} and {@link WriteLock#tryLock()} methods * do not honor this fair setting and will acquire the lock if it is * possible, regardless of waiting threads.) * <p> 所以,如果我哪些地方理解的不对,请指出。
reentrantlock 2014-01-06
  • 打赏
  • 举报
回复
引用 9 楼 Lsheep 的回复:
[quote=引用 8 楼 u013192985 的回复:] [quote=引用 5 楼 Lsheep 的回复:] 首先,下次贴代码请放在标签里,就如我下面展示代码那样; 然后,你下面这块代码是有问题的:有可能某个reader执行synchronized代码之前被调度,另一个reader也进入循环得到同一个元素,最后这个元素被remove了两次,好在remove不存在的元素不会抛异常,只会返回false。

while(container.size()>0){
    String str = container.get(0);
    synchronized(container){ // remove operation need exclusive lock when read-read
        container.remove(str);
    }
最后,我不明白你想干什么,当一个writer在写数据时,其他的reader会被阻塞,当writer执行完后,自动会去唤醒一个reader。你为什么想让writer去主动的notify reader呢?就你给的例子而言,代码中有一些sleep方法,如果你想notify一个sleep线程,那只能interrupte了,所以你到底想干什么呢?
当writer执行完后,自动会去唤醒一个reader? Are you sure? Please note,there is not any wait statement in the run method of read thread,how can the write thread awake the read thread? One word,no "wait",how to wake it? Anyway,thanks for your comments.[/quote] 假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。 如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。[/quote] I feel difficult to communicate with you for this issue. I am not sure whether you don't understand my question or lack of knowledge of technology. However, if you really can help me solve this issue,can you paste your code here?
Lsheep 2014-01-06
  • 打赏
  • 举报
回复
引用 8 楼 u013192985 的回复:
[quote=引用 5 楼 Lsheep 的回复:] 首先,下次贴代码请放在标签里,就如我下面展示代码那样; 然后,你下面这块代码是有问题的:有可能某个reader执行synchronized代码之前被调度,另一个reader也进入循环得到同一个元素,最后这个元素被remove了两次,好在remove不存在的元素不会抛异常,只会返回false。

while(container.size()>0){
    String str = container.get(0);
    synchronized(container){ // remove operation need exclusive lock when read-read
        container.remove(str);
    }
最后,我不明白你想干什么,当一个writer在写数据时,其他的reader会被阻塞,当writer执行完后,自动会去唤醒一个reader。你为什么想让writer去主动的notify reader呢?就你给的例子而言,代码中有一些sleep方法,如果你想notify一个sleep线程,那只能interrupte了,所以你到底想干什么呢?
当writer执行完后,自动会去唤醒一个reader? Are you sure? Please note,there is not any wait statement in the run method of read thread,how can the write thread awake the read thread? One word,no "wait",how to wake it? Anyway,thanks for your comments.[/quote] 假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。 如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。
reentrantlock 2014-01-03
  • 打赏
  • 举报
回复
引用 5 楼 Lsheep 的回复:
首先,下次贴代码请放在标签里,就如我下面展示代码那样; 然后,你下面这块代码是有问题的:有可能某个reader执行synchronized代码之前被调度,另一个reader也进入循环得到同一个元素,最后这个元素被remove了两次,好在remove不存在的元素不会抛异常,只会返回false。

while(container.size()>0){
    String str = container.get(0);
    synchronized(container){ // remove operation need exclusive lock when read-read
        container.remove(str);
    }
最后,我不明白你想干什么,当一个writer在写数据时,其他的reader会被阻塞,当writer执行完后,自动会去唤醒一个reader。你为什么想让writer去主动的notify reader呢?就你给的例子而言,代码中有一些sleep方法,如果你想notify一个sleep线程,那只能interrupte了,所以你到底想干什么呢?
当writer执行完后,自动会去唤醒一个reader? Are you sure? Please note,there is not any wait statement in the run method of read thread,how can the write thread awake the read thread? One word,no "wait",how to wake it? Anyway,thanks for your comments.
reentrantlock 2014-01-03
  • 打赏
  • 举报
回复
引用 5 楼 Lsheep 的回复:
首先,下次贴代码请放在标签里,就如我下面展示代码那样; 然后,你下面这块代码是有问题的:有可能某个reader执行synchronized代码之前被调度,另一个reader也进入循环得到同一个元素,最后这个元素被remove了两次,好在remove不存在的元素不会抛异常,只会返回false。

while(container.size()>0){
    String str = container.get(0);
    synchronized(container){ // remove operation need exclusive lock when read-read
        container.remove(str);
    }
最后,我不明白你想干什么,当一个writer在写数据时,其他的reader会被阻塞,当writer执行完后,自动会去唤醒一个reader。你为什么想让writer去主动的notify reader呢?就你给的例子而言,代码中有一些sleep方法,如果你想notify一个sleep线程,那只能interrupte了,所以你到底想干什么呢?
My question is very simple: how to use notify-wait mechanism to make read thread and write thread communicate each other. If you can do it,would you please improve my sample code? Besides, you can remove the sleep statement in my code,coz I just use it to simulate.
reentrantlock 2014-01-03
  • 打赏
  • 举报
回复
引用 4 楼 xiaoyagouwei 的回复:
English no good!~ 读写锁,写锁在写入数据的时候,读取线程是无法获取该锁的! 所以存在线程在写入集合的时候,其他线程本身就无法读取集合数据,当写入集合完毕时,写锁nulock之后,读取线程会自动获取的到锁。 至于你说的,怎么通知读取线程,告诉他“我写入集合已经完毕”,读写锁实际上不就是这样子吗?只要在写入,没有写完成,都无法读取。 或许没有太明白你的意思是什么? 或者你可以采用: Lock lock = new ReentrantLock(); Condition wirteLock = lock.newCondition(); Condition readLock = lock.newCondition();
To be honest, I feel you don't really understand what is read-write lock in JDK 1.5. The main purpose to use read-write lock instead of reentrantlock is to improve the performance of read thread.Coz multi-threads can read data concurrently when none of write thread is writing data. This is also the difference between read-write lock and reentrantlock. Anyway,I appreciate your attention to my post.
Lsheep 2014-01-03
  • 打赏
  • 举报
回复
首先,下次贴代码请放在标签里,就如我下面展示代码那样; 然后,你下面这块代码是有问题的:有可能某个reader执行synchronized代码之前被调度,另一个reader也进入循环得到同一个元素,最后这个元素被remove了两次,好在remove不存在的元素不会抛异常,只会返回false。

while(container.size()>0){
    String str = container.get(0);
    synchronized(container){ // remove operation need exclusive lock when read-read
        container.remove(str);
    }
最后,我不明白你想干什么,当一个writer在写数据时,其他的reader会被阻塞,当writer执行完后,自动会去唤醒一个reader。你为什么想让writer去主动的notify reader呢?就你给的例子而言,代码中有一些sleep方法,如果你想notify一个sleep线程,那只能interrupte了,所以你到底想干什么呢?
小乞丐_落尘 2014-01-03
  • 打赏
  • 举报
回复
English no good!~ 读写锁,写锁在写入数据的时候,读取线程是无法获取该锁的! 所以存在线程在写入集合的时候,其他线程本身就无法读取集合数据,当写入集合完毕时,写锁nulock之后,读取线程会自动获取的到锁。 至于你说的,怎么通知读取线程,告诉他“我写入集合已经完毕”,读写锁实际上不就是这样子吗?只要在写入,没有写完成,都无法读取。 或许没有太明白你的意思是什么? 或者你可以采用: Lock lock = new ReentrantLock(); Condition wirteLock = lock.newCondition(); Condition readLock = lock.newCondition();
reentrantlock 2014-01-02
  • 打赏
  • 举报
回复
引用 2 楼 lonthy 的回复:
I hava a long time no see the knowledge of threads,at the same time,my ability of Java is limited,Hope the page host don't hate my comment,hehe.make great efforts ,to be a DaShen like LZ.
Anyway,thanks for your attention to this question.
reentrantlock 2013-12-31
  • 打赏
  • 举报
回复
To be honest,I ruminated this question several days.
lonthy 2013-12-31
  • 打赏
  • 举报
回复
I hava a long time no see the knowledge of threads,at the same time,my ability of Java is limited,Hope the page host don't hate my comment,hehe.make great efforts ,to be a DaShen like LZ.

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧