JDK 5.0 Concurrency Utilities 并发处理(3)ReadWriteLock 读写锁

ReadWriteLock 读写锁

 

 

  1package com.vinko.test.concurrent;
  2
  3import java.util.Calendar;
  4import java.util.Map;
  5import java.util.TreeMap;
  6//import java.util.concurrent.locks.Condition;
  7import java.util.concurrent.locks.Lock;
  8import java.util.concurrent.locks.ReentrantReadWriteLock;
  9
 10public class TestReadWriteLock {
 11    
 12    private ReentrantReadWriteLock lock = null;
 13    
 14    private Lock readLock = null;
 15    private Lock writeLock = null;
 16    
 17//    private Condition condition = null;
 18    
 19    public int key = 100;
 20    public int index = 100;
 21    
 22    public Map<Integer, String> dataMap = null;
 23    
 24    public TestReadWriteLock() {
 25        lock = new ReentrantReadWriteLock(true);
 26        
 27        readLock = lock.readLock();
 28        writeLock = lock.writeLock();
 29        
 30//        condition = writeLock.newCondition();
 31        
 32        dataMap = new TreeMap<Integer, String>();
 33    }

 34    
 35    /**
 36     * @param args
 37     */

 38    public static void main(String[] args) {
 39    
 40        TestReadWriteLock tester = new TestReadWriteLock();
 41
 42        // test lock downgrading
 43        
 44//        tester.readLock.lock();
 45//        System.out.println(Thread.currentThread() + " get readLock");
 46
 47        tester.writeLock.lock();
 48        System.out.println(Thread.currentThread() + " get writeLock.");
 49
 50        tester.writeLock.lock();
 51        System.out.println(Thread.currentThread() + " get writeLock.");
 52
 53        tester.readLock.lock();
 54        System.out.println(Thread.currentThread() + " get readLock");
 55
 56        tester.readLock.lock();
 57        System.out.println(Thread.currentThread() + " get readLock");
 58        
 59//        tester.writeLock.lock();
 60//        System.out.println(Thread.currentThread() + " get writeLock.");
 61
 62        tester.readLock.unlock();
 63        tester.readLock.unlock();
 64        tester.writeLock.unlock();
 65        tester.writeLock.unlock();
 66        
 67        tester.test();
 68    }

 69    
 70    public void test() {
 71
 72        for (int i = 0; i < 10; i++{
 73            new Thread(new reader(this)).start();
 74        }

 75        
 76        for (int i = 0; i <3; i++{
 77            new Thread(new writer(this)).start();
 78        }

 79
 80    }

 81
 82    public void read() {
 83/*        
 84        writeLock.lock();
 85        
 86        try {
 87            condition.await();
 88        } catch (InterruptedException e1) {
 89            e1.printStackTrace();
 90        }
 91        
 92        writeLock.unlock();
 93*/

 94        readLock.lock();
 95        
 96        try {
 97            if (dataMap.isEmpty()) {
 98                Calendar now = Calendar.getInstance();
 99                System.out.println(now.getTime() + " R " + Thread.currentThread() + " get key, but map is empty.");
100            }

101            
102            String value = dataMap.get(index);
103
104            Calendar now = Calendar.getInstance();
105            System.out.println(now.getTime() + " R " + Thread.currentThread() + " get key = " + index + " value = " + value + " map size = " + dataMap.size());
106            
107            // get next value
108            if (value != null{
109                index ++;
110            }

111        }
 finally {
112            readLock.unlock();
113        }

114
115        try {
116            Thread.sleep(3000);
117        }
 catch (InterruptedException e) {
118            e.printStackTrace();
119        }

120    }

121    
122    public void write() {
123        
124        writeLock.lock();
125        
126        
127        try {
128            String value = "value" + key;
129            
130            dataMap.put(new Integer(key), value);
131            
132            Calendar now = Calendar.getInstance();
133            System.out.println(now.getTime() + " W " + Thread.currentThread() + " put key = " + key + " value = " + value + " map size = " + dataMap.size());
134            
135            key ++;
136        
137//            condition.signal();
138            
139            try {
140                Thread.sleep(500);
141            }
 catch (InterruptedException e) {
142                e.printStackTrace();
143            }

144
145        }
 finally {
146            writeLock.unlock();
147        }

148
149    }

150}

151
152class reader implements Runnable {
153    
154    private TestReadWriteLock tester = null;
155    
156    public reader(TestReadWriteLock tester) {
157        this.tester = tester;
158    }

159    
160    public void run() {
161        Calendar now = Calendar.getInstance();
162        
163        System.out.println(now.getTime() + " R " + Thread.currentThread() + " started");
164        
165        while (true{
166            tester.read();
167        }

168    }

169}

170
171class writer implements Runnable {
172    
173    private TestReadWriteLock tester = null;
174    
175    public writer(TestReadWriteLock tester) {
176        this.tester = tester;
177    }

178    
179    public void run() {
180        Calendar now = Calendar.getInstance();
181        
182        System.out.println(now.getTime() + " W " + Thread.currentThread() + " started");
183        
184        while (true{
185            tester.write();
186        }

187    }

188}
posted @ 2006-01-24 15:48  kylindai  阅读(1073)  评论(0编辑  收藏  举报