公平锁和非公平锁
公平锁/非公平锁
并发包ReentrantLock的创建可以指定构造函数的boolean类型来得到公平锁或者非公平锁 默认是非公平锁
Java ReentrantLock而言,
通过构造哈数指定该锁是否是公平锁 默认是非公平锁 非公平锁的优点在于吞吐量必公平锁大.
对于synchronized而言 也是一种非公平锁.
可重入锁(又名递归锁)
ReentrantLock/synchronized就是一个典型的可重入锁
可重入锁最大的作用就是避免死锁
自旋锁
独占锁(写)/共享锁(读)/互斥锁
/**
*
资源类
*/
class MyCaChe {
/**
*
保证可见性
*/
private volatile Map<String, Object>
map =
new HashMap<>();
private ReentrantReadWriteLock
reentrantReadWriteLock =
new ReentrantReadWriteLock();
/**
*
写
*
*
@param
key
*
@param
value
*/
public void put(String key, Object value) {
reentrantReadWriteLock.writeLock().lock();
try {
System.
out.println(Thread.
currentThread().getName() +
"
\t
正在写入
" + key);
//
模拟网络延时
try {
TimeUnit.
MICROSECONDS.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
map.put(key, value);
System.
out.println(Thread.
currentThread().getName() +
"
\t
正在完成
");
}
finally {
reentrantReadWriteLock.writeLock().unlock();
}
}
/**
*
读
*
*
@param
key
*/
public void get(String key) {
reentrantReadWriteLock.readLock().lock();
try {
System.
out.println(Thread.
currentThread().getName() +
"
\t
正在读取
");
//
模拟网络延时
try {
TimeUnit.
MICROSECONDS.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
Object result =
map.get(key);
System.
out.println(Thread.
currentThread().getName() +
"
\t
正在完成
" + result);
}
finally {
reentrantReadWriteLock.readLock().unlock();
}
}
public void clearCaChe() {
map.clear();
}
}
/**
* Description:
*
多个线程同时操作 一个资源类没有任何问题 所以为了满足并发量
*
读取共享资源应该可以同时进行
*
但是
*
如果有一个线程想去写共享资源来 就不应该有其他线程可以对资源进行读或写
*
<p>
*
小总结
:
*
读 读能共存
*
读 写不能共存
*
写 写不能共存
*
写操作 原子
+
独占 整个过程必须是一个完成的统一整体 中间不允许被分割 被打断
*
*
@author
veliger@163.com
*
@date
2019-04-13 0:45
**/
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyCaChe myCaChe =
new MyCaChe();
for (
int i = 1; i <= 5; i++) {
final int temp = i;
new Thread(() -> {
myCaChe.put(temp +
"", temp);
}, String.valueOf(i)).start();
}
for (
int i = 1; i <= 5; i++) {
int finalI = i;
new Thread(() -> {
myCaChe.get(finalI +
"");
}, String.valueOf(i)).start();
}
}
}