To be honest,I ruminated this question several days. 

解决方案 »

  1.   

    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.
      

  2.   

    Anyway,thanks for your attention to this question.
      

  3.   

    English no good!~
    读写锁,写锁在写入数据的时候,读取线程是无法获取该锁的!
    所以存在线程在写入集合的时候,其他线程本身就无法读取集合数据,当写入集合完毕时,写锁nulock之后,读取线程会自动获取的到锁。
    至于你说的,怎么通知读取线程,告诉他“我写入集合已经完毕”,读写锁实际上不就是这样子吗?只要在写入,没有写完成,都无法读取。或许没有太明白你的意思是什么?或者你可以采用:
    Lock lock = new ReentrantLock(); 
    Condition wirteLock = lock.newCondition();
    Condition readLock = lock.newCondition();
      

  4.   

    首先,下次贴代码请放在标签里,就如我下面展示代码那样;
    然后,你下面这块代码是有问题的:有可能某个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了,所以你到底想干什么呢?
      

  5.   


    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.
      

  6.   


    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. 
      

  7.   


    当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.
      

  8.   


    当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.
    假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。
    如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。
      

  9.   


    当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.
    假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。
    如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。
    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?
      

  10.   


    当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.
    假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。
    如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。
    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?
    好吧,那我们重新捋一下:你想问的是不是下面两个问题之一,或者都是
    1,如何使用notify
    2,reader和writer是如何通信的
      

  11.   


    当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.
    假设现在系统里只有A,B两个线程,有一个锁L,线程A得到了,然后切换到线程B,线程B等待锁L,接着又切换到A,A释放L,B就会被调度(唤醒这个词或许不太准确),得到L,继续执行。
    如果你想知道reader和writer具体用synchronized还是wait(),notify()方法实现同步的,可以去看看ReentrantReadWriteLock的实现细节。
    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?
    好吧,那我们重新捋一下:你想问的是不是下面两个问题之一,或者都是
    1,如何使用notify
    2,reader和writer是如何通信的
    Yeah,but the pre-condition is you must use ReentrantReadWriteLock to implement.
      

  12.   

    I feel no difficulty to implement it if I use ReentrantLock or traditional synchroniz ation mechanism instead of ReentrantReadWriteLock.
      

  13.   

    @Lsheep, please modify my source code if you are confident of your technology.
    Thanks a million in advance.
      

  14.   

    可能我还是没明白你的意思,下面这段话:
    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>
    所以,如果我哪些地方理解的不对,请指出。