/**
* @Auther: Shen_hs
* @Date: 2021/5/1
* @Description: 获取锁失败时的异常
*/
public class GetThreadException extends Exception {
public GetThreadException() {
super();
}
public GetThreadException(String message) {
super(message);
}
}
//======================================
/**
* @Auther: Shen_hs
* @Date: 2021/5/1
* @Description: 利用CAS自定义显示锁
*/
public class CompareAndSetLock {
private final AtomicInteger value = new AtomicInteger(0);
//表示被锁住的线程
private Thread lockedThread;
/**
* 加锁
*/
public void tryLock() throws GetThreadException {
boolean success = value.compareAndSet(0, 1); //和期望值一样 返回true
if(!success)
throw new GetThreadException(Thread.currentThread().getName()+ " get the lock failed"); //与期望值不一样抛出异常
else
lockedThread = Thread.currentThread();
}
/**
* 解锁
*/
public void unLock(){
if(0==value.get()){
return; //如果为0 不用解锁 直接返回
}
//只有锁住的线程为当前线程 才能解锁 避免被其他线程释放锁
if(lockedThread == Thread.currentThread())
value.compareAndSet(1,0); //解锁 将1变成0
}
}
//=============================
public class AtomicIntegerTest2 {
private static final CompareAndSetLock LOCK = new CompareAndSetLock();
public static void main(String[] args) {
IntStream.rangeClosed(0,5).forEach(i->new Thread(() -> {
try {
doSomething2();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (GetThreadException e) {
e.printStackTrace();
}
}).start());
}
public static void doSomething() throws InterruptedException {
synchronized (AtomicIntegerTest2.class) {
System.out.println(Thread.currentThread().getName()+" get the lock");
Thread.sleep(10_000);
}
}
public static void doSomething2() throws InterruptedException, GetThreadException {
try {
LOCK.tryLock();
System.out.println(Thread.currentThread().getName()+" get the lock");
Thread.sleep(10_000);
} finally {
LOCK.unLock();
}
}
}
//===========================OUTPUT(只有一个线程抢到锁 其他直接返回)
Thread-0 get the lock
com.wangwenjun.atomic.GetThreadException: Thread-2 get the lock failed
at com.wangwenjun.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:25)
at com.wangwenjun.atomic.AtomicIntegerTest2.doSomething2(AtomicIntegerTest2.java:38)
at com.wangwenjun.atomic.AtomicIntegerTest2.lambda$null$0(AtomicIntegerTest2.java:18)
at java.lang.Thread.run(Thread.java:748)
com.wangwenjun.atomic.GetThreadException: Thread-4 get the lock failed
at com.wangwenjun.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:25)
at com.wangwenjun.atomic.AtomicIntegerTest2.doSomething2(AtomicIntegerTest2.java:38)
at com.wangwenjun.atomic.AtomicIntegerTest2.lambda$null$0(AtomicIntegerTest2.java:18)
at java.lang.Thread.run(Thread.java:748)
com.wangwenjun.atomic.GetThreadException: Thread-5 get the lock failed
at com.wangwenjun.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:25)
at com.wangwenjun.atomic.AtomicIntegerTest2.doSomething2(AtomicIntegerTest2.java:38)
at com.wangwenjun.atomic.AtomicIntegerTest2.lambda$null$0(AtomicIntegerTest2.java:18)
at java.lang.Thread.run(Thread.java:748)
com.wangwenjun.atomic.GetThreadException: Thread-3 get the lock failed
at com.wangwenjun.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:25)
at com.wangwenjun.atomic.AtomicIntegerTest2.doSomething2(AtomicIntegerTest2.java:38)
at com.wangwenjun.atomic.AtomicIntegerTest2.lambda$null$0(AtomicIntegerTest2.java:18)
at java.lang.Thread.run(Thread.java:748)
com.wangwenjun.atomic.GetThreadException: Thread-1 get the lock failed
at com.wangwenjun.atomic.CompareAndSetLock.tryLock(CompareAndSetLock.java:25)
at com.wangwenjun.atomic.AtomicIntegerTest2.doSomething2(AtomicIntegerTest2.java:38)
at com.wangwenjun.atomic.AtomicIntegerTest2.lambda$null$0(AtomicIntegerTest2.java:18)
at java.lang.Thread.run(Thread.java:748)
java原子类:利用CAS自定义显示锁
最新推荐文章于 2024-08-16 23:08:28 发布