ReadWriteLock读写锁
多个线程同时读一个资源类没有任何问题,所以为了满足并发量,读取共享资源应该可以同时进行。 但是有一个线程想去写共享资源,就不应该再有其他线程可以对该资源进行读或写 总结: 读读----能共存 读写----不能共存 写写----不能共存
class MyCache {
private volatile Map< String, Object> map = new HashMap < > ( ) ;
public void put ( String key, Object value) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 正在写" + key) ;
try { TimeUnit. MILLISECONDS. sleep ( 300 ) ; } catch ( InterruptedException e) { e. printStackTrace ( ) ; }
map. put ( key, value) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 写完了" + key) ;
}
public Object get ( String key) {
Object result = null;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 正在读" + key) ;
try {
TimeUnit. MILLISECONDS. sleep ( 300 ) ; } catch ( InterruptedException e) { e. printStackTrace ( ) ; }
result = map. get ( key) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 读完了" + result) ;
return result;
}
}
public class ReadWriteLockDemo {
public static void main ( String[ ] args) {
MyCache myCache = new MyCache ( ) ;
for ( int i = 1 ; i <= 5 ; i++ ) {
final int num = i;
new Thread ( ( ) - > {
myCache. put ( num+ "" , num+ "" ) ;
} , String. valueOf ( i) ) . start ( ) ;
}
for ( int i = 1 ; i <= 5 ; i++ ) {
final int num = i;
new Thread ( ( ) - > {
myCache. get ( num+ "" ) ;
} , String. valueOf ( i) ) . start ( ) ;
}
}
}
1 正在写1
2 正在写2
3 正在写3
4 正在写4
5 正在写5
1 正在读1
2 正在读2
3 正在读3
4 正在读4
5 正在读5
4 写完了4
1 写完了1
2 写完了2
3 写完了3
5 写完了5
1 读完了null
2 读完了2
4 读完了4
3 读完了3
5 读完了5
class MyCache {
private volatile Map< String, Object> map = new HashMap < > ( ) ;
private ReadWriteLock rwLock = new ReentrantReadWriteLock ( ) ;
public void put ( String key, Object value) {
rwLock. writeLock ( ) . lock ( ) ;
try {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 正在写" + key) ;
try {
TimeUnit. MILLISECONDS. sleep ( 300 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
map. put ( key, value) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 写完了" + key) ;
System. out. println ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
rwLock. writeLock ( ) . unlock ( ) ;
}
}
public Object get ( String key) {
rwLock. readLock ( ) . lock ( ) ;
Object result = null;
try {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 正在读" + key) ;
try {
TimeUnit. MILLISECONDS. sleep ( 300 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
result = map. get ( key) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "\t 读完了" + result) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
rwLock. readLock ( ) . unlock ( ) ;
}
return result;
}
}
public class ReadWriteLockDemo {
public static void main ( String[ ] args) {
MyCache myCache = new MyCache ( ) ;
for ( int i = 1 ; i <= 5 ; i++ ) {
final int num = i;
new Thread ( ( ) - > {
myCache. put ( num + "" , num + "" ) ;
} , String. valueOf ( i) ) . start ( ) ;
}
for ( int i = 1 ; i <= 5 ; i++ ) {
final int num = i;
new Thread ( ( ) - > {
myCache. get ( num + "" ) ;
} , String. valueOf ( i) ) . start ( ) ;
}
}
}
1 正在写1
1 写完了1
2 正在写2
2 写完了2
3 正在写3
3 写完了3
4 正在写4
4 写完了4
5 正在写5
5 写完了5
1 正在读1
2 正在读2
5 正在读5
3 正在读3
4 正在读4
1 读完了1
2 读完了2
4 读完了4
3 读完了3
5 读完了5