想实现:
多个线程读,多个线程写;
只要有一个写线程获得资源,其他读线程和写线程都等待;而多个读线程可以同时读取,即读线程之间不需要互斥。
应该怎么做?
谢谢!!!!!!

解决方案 »

  1.   

    1,写的那个方法上面加上synchronized,这样,每次只有一个进程可以访问这个方法,以此来实现单写。
    读的方法就不管了,反正也不会更改数据。2,或者定一个static变量来记录要写的进程数,当这个变量大于0的时候,写进程不能进行,只有等待其它进程写完。某一正在进行的写进程结束后,检查此变量,如果为0,则唤醒正在等待的一个写进程。读进程依然不用管,还是不更新数据的原因 。
      

  2.   

    只要有一个写线程获得资源,其他读线程和写线程都等待;
    而多个读线程可以同时读取,即读线程之间不需要互斥。写线程锁资源。同时方法加synchronized
    读线程判断资源是否被锁,仅此而已,无需加锁和synchronized.有一个问题:有读线程在读,这时候来了一个写线程怎么办?
      

  3.   

    不知道好使不/*
     * file: ReadWriteLock.java
     * class: ReadWriteLock
     *
     * description: 
     *
     * @author:  leisore
     * @version: V1.0.0
     */
    package cn.leisore.daily._2010_08_02;import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLock {    public static void main(String[] args) {        Cache cache = new Cache();
            ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();        ExecutorService pool = Executors.newCachedThreadPool();        // 3 writers 7 readers
            for (int i = 0; i < 10; i++) {
                if (i > 3) {
                    pool.submit(new Reader(rwLock.readLock(), cache));
                } else {
                    pool.submit(new Writer(rwLock.writeLock(), cache));
                }
            }        try {
                Thread.sleep(1000 * 60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                pool.shutdownNow();
            }
        }    static class Cache {
            String cache = "init";        public String toString() {
                return cache;
            }        public Cache refreshWith(String cache) {
                this.cache = cache;
                return this;
            }
        }    static class Reader implements Runnable {        static int counter = 0;
            final int id = counter++;
            Lock lock = null;
            Cache cache = null;        public Reader(Lock lock, Cache cache) {
                this.lock = lock;
                this.cache = cache;
            }        public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    lock.lock();
                    try {
                        Thread.sleep(200);
                        System.out.println("Reader " + id + " read cache: "
                                + cache.toString());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                        Thread.yield();
                    }
                }
            }
        }    static class Writer implements Runnable {        static int counter = 0;
            final int id = counter++;
            Lock lock = null;
            Cache cache = null;        public Writer(Lock lock, Cache cache) {
                this.lock = lock;
                this.cache = cache;
            }        public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    lock.lock();
                    try {
                        Thread.sleep(500);
                        System.out.println("Writer " + id + " writer cache: "
                                + cache.refreshWith("Writer " + id));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        lock.unlock();
                        Thread.yield();
                    }
                }
            }
        }
    }