Java高并发26-释放锁以及ReentrantLock实例演示
2021-06-04 03:02
import java.util.ArrayList; public static class ReentrantLockList { //线程不安全的Listpackage com.ruigege.LockSourceAnalysis6;
private ArrayList
//独占锁
private volatile ReentrantLock lock = new ReentrantLock();
//添加元素
public void add(String e) {
lock.lock();
try {
array.add(e);
}finally {
lcok.unlock();
}
}
//删除元素
public void remove(String e) {
lock.lock();
try {
array.remove(e);
}finally {
lock.unlock();
}
}
//获取数据
public String get(int index) {
lock.lock();
try {
return array.get(index);
}finally {
lock.unlock();
}
}
}
文章标题:Java高并发26-释放锁以及ReentrantLock实例演示
文章链接:http://soscw.com/essay/90216.html