标签:
package cn.dyg; import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; public class ThreadLock { public static void main(String[] args) { final Business business = new BusinessWriteReadLock(); for (int i = 0; i < 3; i++) { new Thread(new Runnable() { @Override public void run() { while(true) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(business.get("zjy")); } } }).start(); } new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } business.put("zjy", "zhaojunyang"); } }).start(); } } interface Business { public void put(String key, String value); public String get(String key); } class BusinessNoSynchronized implements Business { private Map<String, String> map = new HashMap<String, String>(); @Override public void put(String key, String value) { System.out.println(Thread.currentThread().getName() + "write shared data..."); map.put(key, value); } @Override public String get(String key) { System.out.println(Thread.currentThread().getName() + "read shared data..."); return map.get(key); } } class BusinessSynchroinzed implements Business { private Map<String, String> map = new HashMap<String, String>(); @Override public synchronized String get(String key) { String value = map.get(key); while (value == null) { try { wait(); value = map.get(key); } catch (InterruptedException e) { e.printStackTrace(); } } return map.get(key); } @Override public synchronized void put(String key, String value) { map.put(key, value); notify(); } } class BusinessLock implements Business { private Map<String, String> map = new HashMap<String, String>(); private ReentrantLock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void put(String key, String value) { lock.lock(); try { System.out.println(Thread.currentThread().getName() + "write shared data..."); map.put(key, value); condition.signal(); } finally { lock.unlock(); } } public String get(String key) { lock.lock(); try { System.out.println(Thread.currentThread().getName() + "read shared data..."); while (map.get(key) == null) { try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } return map.get(key); } finally { lock.unlock(); } } } //这个是遗留问题,怎么实现阻塞并释放读写锁,这个一直比较困惑,希望路过的大神给予指导?? class BusinessWriteReadLock implements Business { private Map<String, String> map = new HashMap<String, String>(); private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private WriteLock writeLock = lock.writeLock(); private ReadLock readLock = lock.readLock(); @Override public void put(String key, String value) { writeLock.lock(); try { map.put(key, value); } finally { writeLock.unlock(); } } @Override public String get(String key) { readLock.lock(); try { while (map.get(key) == null) { Thread.yield(); //并不释放当前锁 } return map.get(key); } finally { readLock.unlock(); } } }
标签:
原文地址:http://www.cnblogs.com/zhaojunyang/p/5063802.html