标签:重入 最大 next sleep dex 之间 ant 也会 void
public class ReadWriteLockDemo { private static Lock lock = new ReentrantLock(); private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private static Lock readLock = readWriteLock.readLock(); private static Lock writeLock = readWriteLock.writeLock(); private int value; public Object handleRead(Lock lock) throws InterruptedException { try { lock.lock(); Thread.sleep(1000);//模拟读操作 return value; } finally { lock.unlock(); } } public void handleWrite(Lock lock, int index) throws InterruptedException { try { lock.lock(); Thread.sleep(1000); this.value = index; } finally { lock.unlock(); } } public static void main(String[] args) { final ReadWriteLockDemo demo = new ReadWriteLockDemo(); Runnable readRunnable = () -> { try { Object o = demo.handleRead(readLock);//读写锁 // Object o1 = demo.handleRead(lock);//普通锁 } catch (InterruptedException e) { e.printStackTrace(); } }; Runnable writeRunnable = () -> { try { demo.handleWrite(writeLock, new Random().nextInt());//读写锁 //demo.handleWrite(lock, new Random().nextInt());//普通锁 } catch (InterruptedException e) { e.printStackTrace(); } }; for (int i = 0; i < 18; i++) { new Thread(readRunnable).start(); } for (int i = 18; i < 20; i++) { new Thread(writeRunnable).start(); } } }
标签:重入 最大 next sleep dex 之间 ant 也会 void
原文地址:http://www.cnblogs.com/ten951/p/6212139.html