逐梦少年,看你能不能发现宝藏(13)


读锁的共享锁可保证并发读是非常高效的,读写,写读,写写的过程是互斥的 。
锁的演变过程:sync —> Lock ------>ck
class MyCahe {//资源类private volatile Map map = new HashMap<>();//private Lock lock = new ReentrantLock();private ReentrantReadWriteLock lock =new ReentrantReadWriteLock();public void put(String key, String value) throws InterruptedException {lock.writeLock().lock();try{System.out.println(Thread.currentThread().getName()+"\t 正在写入:"+key);Thread.sleep(300);map.put(key,value);System.out.println(Thread.currentThread().getName()+"\t 写入完成");}catch(Exception e){e.printStackTrace();}finally{lock.writeLock().unlock();}}public void get(String key) throws InterruptedException {lock.readLock().lock();try{System.out.println(Thread.currentThread().getName()+"\t 正在读取:"+key);Thread.sleep(300);Object result = map.get(key);System.out.println(Thread.currentThread().getName()+"\t 读取完成");}catch(Exception e){e.printStackTrace();}finally{lock.readLock().unlock();}}}/** 多个线程同时读一个资源类没有任何问题,所以为了满足并发量,读取共享资源应该可以同时进行 。* 但是* 如果有一个线程想去写共享资源来,就不应该再有其他线程可以对该资源进行读或写* 小总结:*读-读能共存*读-写不能共存*写-写不能共存**写操作: 原子 + 独占,整个过程必须是一个完整的统一体,中间不许被分割,被打断 。* */public class ReentrantReadWriteLockDemo {public static void main(String[] args) {MyCahe myCahe = new MyCahe();for (int i = 0; i < 5; i++) {final int tempInt = i;new Thread(()->{try {myCahe.put(tempInt+"", tempInt+"");} catch (InterruptedException e) {e.printStackTrace();}},i+"").start();}for (int i = 0; i < 5; i++) {final int tempInt = i;new Thread(()->{try {myCahe.get(tempInt + "");} catch (InterruptedException e) {e.printStackTrace();}},i+"").start();}}}===================================================================================0正在写入:00写入完成1正在写入:11写入完成2正在写入:22写入完成3正在写入:33写入完成4正在写入:44写入完成0正在读取:01正在读取:12正在读取:23正在读取:34正在读取:43读取完成4读取完成1读取完成2读取完成0读取完成
28.//使用过么?—JUC关键的包
枚举小技巧,长见识了 。。。----》让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒,主要有两个方法,当一个或多个线程调用方法会将计数器减1(调用方法的线程不会阻塞),当计数器的值变为零时,因调用await()方法被阻塞的线程就会被唤醒,继续执行
/*** 枚举是1.5出的新的特性*/public enum CountryEnum {ONE(1, "齐"), TWO(2, "楚"), THREE(3, "燕"), FOUR(4, "赵"), FIVE(5, "魏"), SIX(6, "韩");CountryEnum(Integer retCode, String retMessage) {this.retCode = retCode;this.retMessage = retMessage;}@Getterprivate Integer retCode;@Getterprivate String retMessage;public static CountryEnum forEach_CountryEnum(int index) {CountryEnum[] values = CountryEnum.values();for (CountryEnum value : values) {if (index == value.getRetCode()) {return value;}}return null;}}====================================================================================public class CountDownLatchDemo {public static void main(String[] args) throws InterruptedException {CountDownLatch countDownLatch = new CountDownLatch(6);for (int i = 1; i <= 6; i++) {new Thread(()->{System.out.println(Thread.currentThread().getName()+"\t 国,被灭");countDownLatch.countDown();},CountryEnum.forEach_CountryEnum(i).getRetMessage()).start();}countDownLatch.await();System.out.println(Thread.currentThread().getName()+"\t **************秦帝国,一统华夏");/*楚国,被灭赵国,被灭齐国,被灭魏国,被灭燕国,被灭韩国,被灭main**************秦帝国,一统华夏*/}private static void closerDoor() throws InterruptedException {CountDownLatch countDownLatch = new CountDownLatch(6);for (int i = 0; i