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

本文介绍了一个使用ReentrantReadWriteLock实现的读写锁示例。该示例通过多个读取线程与写入线程并发访问共享资源的方式,展示了读写锁在实际应用中的工作原理。读操作可以并行进行,但写操作会阻止其他读取和写入。

ReadWriteLock 读写锁

 

 

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

 34InBlock.gif    
 35ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
 36InBlock.gif     * @param args
 37ExpandedSubBlockEnd.gif     */

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

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

 75InBlock.gif        
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        for (int i = 0; i <3; i++dot.gif{
 77InBlock.gif            new Thread(new writer(this)).start();
 78ExpandedSubBlockEnd.gif        }

 79InBlock.gif
 80ExpandedSubBlockEnd.gif    }

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

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

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

111ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 finally dot.gif{
112InBlock.gif            readLock.unlock();
113ExpandedSubBlockEnd.gif        }

114InBlock.gif
115ExpandedSubBlockStart.gifContractedSubBlock.gif        try dot.gif{
116InBlock.gif            Thread.sleep(3000);
117ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (InterruptedException e) dot.gif{
118InBlock.gif            e.printStackTrace();
119ExpandedSubBlockEnd.gif        }

120ExpandedSubBlockEnd.gif    }

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

144InBlock.gif
145ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 finally dot.gif{
146InBlock.gif            writeLock.unlock();
147ExpandedSubBlockEnd.gif        }

148InBlock.gif
149ExpandedSubBlockEnd.gif    }

150ExpandedBlockEnd.gif}

151None.gif
152ExpandedBlockStart.gifContractedBlock.gifclass reader implements Runnable dot.gif{
153InBlock.gif    
154InBlock.gif    private TestReadWriteLock tester = null;
155InBlock.gif    
156ExpandedSubBlockStart.gifContractedSubBlock.gif    public reader(TestReadWriteLock tester) dot.gif{
157InBlock.gif        this.tester = tester;
158ExpandedSubBlockEnd.gif    }

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

168ExpandedSubBlockEnd.gif    }

169ExpandedBlockEnd.gif}

170None.gif
171ExpandedBlockStart.gifContractedBlock.gifclass writer implements Runnable dot.gif{
172InBlock.gif    
173InBlock.gif    private TestReadWriteLock tester = null;
174InBlock.gif    
175ExpandedSubBlockStart.gifContractedSubBlock.gif    public writer(TestReadWriteLock tester) dot.gif{
176InBlock.gif        this.tester = tester;
177ExpandedSubBlockEnd.gif    }

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

187ExpandedSubBlockEnd.gif    }

188ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/kylindai/archive/2006/01/24/322667.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值