Java并发

This commit is contained in:
罗祥
2019-11-26 14:45:55 +08:00
parent 9b162124a8
commit 3884f30ffd
6 changed files with 386 additions and 7 deletions

View File

@ -70,7 +70,7 @@ public class ReadWriteLock {
public static void main(String[] args) throws InterruptedException {
// 耗时2秒写锁是互斥的,但读锁是并行的
// 写锁是排它的,但读锁是共享的耗时3秒左右
for (int j = 0; j < 2; j++) {
Thread thread = new Thread(new Write(writeLock, String.valueOf(j)));
thread.start();
@ -81,7 +81,7 @@ public class ReadWriteLock {
}
// 使用重入锁时,读锁彼此之间也是互斥的
// 使用重入锁时耗时20秒左右
for (int j = 0; j < 2; j++) {
Thread thread = new Thread(new Write(reentrantLock, String.valueOf(j)));
thread.start();