关于java读写锁的测试
关于java读写锁的测试
package com.zang.concurrent.locks.rwlock; import java.util.Random; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * 读写锁测试 * 写锁可降级为读锁 * 读锁可重用 * * @author Zhang Qiang * @Date 2019/9/17 17:04 */ public class ReadWriteLockUsage { private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private final Lock readLock = readWriteLock.readLock(); private final Lock writeLock = readWriteLock.writeLock(); private static StringBuilder argString = new StringBuilder(); public static void main(String[] args) { ReadWriteLockUsage readWriteLockUsage = new ReadWriteLockUsage(); // 循环创建线程获取锁 for (int i = 0; i < 10; i++) { long next = (long) (new Random().nextFloat() * 1000); Thread writeThread = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(next); argString.delete(0,argString.length()); argString.append(next); System.out.println(Thread.currentThread().getName() + " -> start" + ", next :" + next + ", argString:" + argString); readWriteLockUsage.wreiter(argString.toString()); } catch (InterruptedException e) { e.printStackTrace(); } } }); writeThread.setName("T_"+i); writeThread.start(); Thread readThread = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(next); readWriteLockUsage.reader(argString.toString()); } catch (InterruptedException e) { e.printStackTrace(); } } }); readThread.start(); } } /** * 读锁 * * @param argString * @return */ public void wreiter(String argString){ if (writeLock.tryLock()){ try { argString += "_" +ClassLoader.getSystemClassLoader().getParent().toString(); Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } finally { writeLock.unlock(); System.out.println("writeLock 已释放"); } } else { System.out.println("未获取到写锁"); } } /** * 写锁 * * @param argString * @return */ public void reader(String argString){ readLock.lock(); try { System.out.println("readLock ============== atgString: " + argString); } finally { readLock.unlock(); } } }
打印信息:
"D:\Program Files\Java\jdk1.8.0_202\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\lib\idea_rt.jar=56588:D:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_202\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar;D:\IdeaProject\github\nettyGui\target\classes;D:\Maven\Repository\io\netty\netty-all\5.0.0.Alpha2\netty-all-5.0.0.Alpha2.jar;D:\Maven\Repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;D:\Maven\Repository\com\google\protobuf\protobuf-java\3.7.1\protobuf-java-3.7.1.jar;D:\Maven\Repository\redis\clients\jedis\2.9.0\jedis-2.9.0.jar;D:\Maven\Repository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;D:\Maven\Repository\commons-io\commons-io\2.4\commons-io-2.4.jar;D:\Maven\Repository\com\github\javafaker\javafaker\1.0.0\javafaker-1.0.0.jar;D:\Maven\Repository\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;D:\Maven\Repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23-android.jar;D:\Maven\Repository\com\github\mifmif\generex\1.0.2\generex-1.0.2.jar;D:\Maven\Repository\dk\brics\automaton\automaton\1.11-8\automaton-1.11-8.jar;D:\Maven\Repository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\Maven\Repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\Maven\Repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;D:\Maven\Repository\org\quartz-scheduler\quartz\2.3.0\quartz-2.3.0.jar;D:\Maven\Repository\com\mchange\c3p0\0.9.5.2\c3p0-0.9.5.2.jar;D:\Maven\Repository\com\mchange\mchange-commons-java\0.2.11\mchange-commons-java-0.2.11.jar;D:\Maven\Repository\com\zaxxer\HikariCP-java6\2.3.13\HikariCP-java6-2.3.13.jar;D:\Maven\Repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar;D:\Maven\Repository\org\slf4j\slf4j-nop\1.7.2\slf4j-nop-1.7.2.jar;D:\Maven\Repository\com\aliyun\aliyun-java-sdk-core\3.0.0\aliyun-java-sdk-core-3.0.0.jar;D:\Maven\Repository\com\aliyun\aliyun-java-sdk-dm\3.1.0\aliyun-java-sdk-dm-3.1.0.jar;D:\Maven\Repository\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar" com.zang.concurrent.locks.rwlock.ReadWriteLockUsage T_9 -> start, next :18, argString:18 readLock ============== atgString: 未获取到写锁 readLock ============== atgString: T_5 -> start, next :174, argString:174 T_6 -> start, next :364, argString:364 未获取到写锁 writeLock 已释放 readLock ============== atgString: 174 T_8 -> start, next :529, argString:529 T_3 -> start, next :622, argString:622 未获取到写锁 T_4 -> start, next :637, argString:637 未获取到写锁 T_7 -> start, next :661, argString:661 未获取到写锁 readLock ============== atgString: 529 readLock ============== atgString: 622 readLock ============== atgString: 622 readLock ============== atgString: 637 writeLock 已释放 T_0 -> start, next :750, argString:750 readLock ============== atgString: 750 未获取到写锁 T_1 -> start, next :773, argString:773 writeLock 已释放 readLock ============== atgString: 773 T_2 -> start, next :985, argString:985 writeLock 已释放 readLock ============== atgString: 985 Process finished with exit code 0
posted on 2019-09-17 17:58 GhostSugar 阅读(380) 评论(0) 编辑 收藏 举报