java的锁机制
主要有2种锁:1. 悲观锁; 2. 乐观锁。
悲观锁:
一个典型的倚赖数据库的悲观锁调用:
select * from city where cityName="BEIJING" for update
这条 sql 语句锁定了 city表中所有符合检索条件( cityName=“BEIJING” )的记录。 本次事务提交之前(事务提交时会释放事务过程中的锁),外界无法修改这些记录。
而JAVA上悲观锁主要指的是Synchronized
JAVA上的乐观锁指的是自旋锁,涉及CAS机制
悲观的阻塞锁,在请求不到资源时会将线程置为挂起状态,等待系统再次调用。操作系统实现线程之间的切换时需要从用户态(运行用户程序)转换到核心态(运行操作系统程序),这个状态之间的转换需要相对比较长的时间,时间成本相对较高。
乐观的自旋锁在申请不到资源时,会循环等待资源释放。会占用CPU资源。相对悲观锁而言,乐观锁机制采取了更加宽松的加锁机制。
下面的代码是个简单的自旋锁,(其中使用了Atomic原子对象)
import java.util.concurrent.atomic.AtomicReference;
public class C {
private AtomicReference<Thread> cas = new AtomicReference<Thread>();
private int count ;
public void lock(){
//返回的是 一个实例。 这个实例是当前Thread 的引用
Thread current = Thread.currentThread();
if(current == cas.get()){
count++;
return;
}
while (!cas.compareAndSet(null, current)) {
try {
current.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("等待。。。");
// DO nothing
}
}
public void unlock() {
Thread current = Thread.currentThread();
if(current == cas.get()){
if(count>0){
count--;
}else{
cas.compareAndSet(current, null);
}
}
}
}
下面是模拟调用锁,主线程先请求锁资源,然后启动一个线程,再次请求锁资源(抢占资源)
main:
public class MainTest {
public static void main(String[] args) {
System.out.println("自旋锁实例");
C c = new C();
c.lock();
ThreadA a = new ThreadA();
a.c = c;
a.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("第一个线程解锁");
c.unlock();
}
}
线程抢锁资源
public class ThreadA extends Thread{
public C c;
public void run(){
System.err.println("第二个线程调用锁");
this.c.lock();
System.err.println("第二个线程调用锁结束");
/*System.err.println("解锁成功");*/
}
}