标签:executors pool ring imp 使用 unit nbsp ice string
悲观读(写的机会很少)
package com.dwz.stampedLock; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.StampedLock; import java.util.stream.Collectors; /** * 悲观读(写的机会很少) * 100 threads * 99 threads need read lock * 1 threads need write lock * 可以使用StampedLock替代ReentrantReadWriteLock */ public class StampedLockExample { private final static StampedLock lock = new StampedLock(); private final static List<Long> DATA = new ArrayList<>(); private static void read() { long stamped = -1; try { stamped = lock.readLock(); Optional.of(DATA.stream().map(String::valueOf).collect(Collectors.joining("#", "R-", ""))) .ifPresent(System.out::println); TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlockRead(stamped); } } private static void write() { long stamped = -1; try { stamped = lock.writeLock(); DATA.add(System.currentTimeMillis()); TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlockWrite(stamped); } } public static void main(String[] args) { final ExecutorService executor = Executors.newFixedThreadPool(10); Runnable readTask = () -> { for(;;) { read(); } }; Runnable writeTask = () -> { for(;;) { write(); } }; executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(writeTask); } }
改进
package com.dwz.stampedLock; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.StampedLock; import java.util.stream.Collectors; /** * 乐观读(写的机会相对增多) */ public class StampedLockExample2 { private final static StampedLock lock = new StampedLock(); private final static List<Long> DATA = new ArrayList<>(); private static void read() { long stamp = lock.tryOptimisticRead(); if(lock.validate(stamp)) {//验证在该时间锁是否还持有 try { stamp = lock.readLock(); Optional.of(DATA.stream().map(String::valueOf).collect(Collectors.joining("#", "R-", ""))) .ifPresent(System.out::println); TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlockRead(stamp); } } } private static void write() { long stamped = -1; try { stamped = lock.writeLock(); DATA.add(System.currentTimeMillis()); TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlockWrite(stamped); } } public static void main(String[] args) { final ExecutorService executor = Executors.newFixedThreadPool(10); Runnable readTask = () -> { for(;;) { read(); } }; Runnable writeTask = () -> { for(;;) { write(); } }; executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(readTask); executor.submit(writeTask); } }
使用StampedLock替代ReentrantReadWriteLock
标签:executors pool ring imp 使用 unit nbsp ice string
原文地址:https://www.cnblogs.com/zheaven/p/13396177.html