如何实现一个自旋分布式锁
如何实现一个自旋分布式锁,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
成都创新互联-专业网站定制、快速模板网站建设、高性价比新蔡网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式新蔡网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖新蔡地区。费用合理售后完善,十年实体公司更值得信赖。
AQS的全称为AbstractQueuedSynchronizer,抽象队列同步器
在ReentrantLock类中,我们来看一下加锁是怎么来实现的。
private final Sync sync;
public void lock() {sync.lock();}
这个sync就是一个AQS的子类,并且是一个抽象类
abstract static class Sync extends AbstractQueuedSynchronizer
它的lock()方法是一个抽象方法
abstract void lock();
具体实现sync的是两个子类,公平锁类
static final class FairSync extends Sync
和非公平锁类
static final class NonfairSync extends Sync
这里我们主要以非公平锁来说明,因为我们平常用的大部分都是非公平锁,在非公平锁中,lock()方法的实现如下
final void lock() { //AQS的内部方法,无锁竞争AQS中state的状态,state的初始值为0,获得锁的将0变为1if (compareAndSetState(0, 1)) //竞争到state为1的将当前线程设为AQS的独家主线程setExclusiveOwnerThread(Thread.currentThread()); else acquire(1);}
在AbstractQueuedSynchronizer类中
private static final long stateOffset;
在静态代码块中,我们可以看到这个stateOffset取的就是state,并且这个state是多线程可见的volatile。
stateOffset = unsafe.objectFieldOffset (AbstractQueuedSynchronizer.class.getDeclaredField("state"));
private volatile int state;
protected final boolean compareAndSetState(int expect, int update) {// See below for intrinsics setup to support this return unsafe.compareAndSwapInt(this, stateOffset, expect, update);}
private transient Thread exclusiveOwnerThread;
protected final void setExclusiveOwnerThread(Thread thread) {exclusiveOwnerThread = thread;}
这里unsafe.compareAndSwapInt()是用C来实现的,我们可以用java来模拟该方法
@Slf4j@Getterpublic class GetState {private AtomicReferencestate = new AtomicReference<>(0); private boolean lockState() {while (true) {if (state.compareAndSet(0,1)) {return true; } } }private void unlockState() {state.set(0); }@AllArgsConstructor private static class Task implements Runnable {private GetState getState; @Override public void run() {if (getState.lockState()) {log.info(Thread.currentThread().getName() + "获取锁"); } } }public static void main(String[] args) throws InterruptedException { ExecutorService service = Executors.newFixedThreadPool(16); GetState state = new GetState(); for (int i = 0;i < 10;i++) { service.execute(new Task(state)); }while (state.getState().get() == 1) { Thread.sleep(1000); state.unlockState(); } service.shutdown(); } }
打印日志(每秒打印一条)
15:35:42.953 [pool-1-thread-1] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-1获取锁
15:35:43.953 [pool-1-thread-9] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-9获取锁
15:35:44.957 [pool-1-thread-5] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-5获取锁
15:35:45.962 [pool-1-thread-2] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-2获取锁
15:35:46.962 [pool-1-thread-7] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-7获取锁
15:35:47.962 [pool-1-thread-3] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-3获取锁
15:35:48.967 [pool-1-thread-8] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-8获取锁
15:35:49.969 [pool-1-thread-6] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-6获取锁
15:35:50.970 [pool-1-thread-4] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-4获取锁
15:35:51.971 [pool-1-thread-10] INFO com.guanjian.websocket.tomic.GetState - pool-1-thread-10获取锁
Process finished with exit code 0
现在我们可以来写一个支持自旋的分布式锁了。
public class SpinDistributedLock {private volatile AtomicReferencestate = new AtomicReference<>(false); public boolean lock(redisService redisService,String key,String value,int expire) {while (true) {if (state.compareAndSet(false, RedisTool.tryGetDistributedLock(redisService,key,value,expire))) {if (state.get()) {return true; } } } }public void unlock(RedisService redisService,String key,String value) {state.set(!RedisTool.releaseDistributedLock(redisService,key,value)); } }
常规分布式锁可以参考采用redis token,分布式锁的接口幂等性实现
现在我们来进行一个简单的测试,先不使用分布式锁
我们在redis中手动设置一个键count,0
127.0.0.1:6379> set count 0
OK
我们的目的是累加这个count,但不能让其超过10
@Servicepublic class NoDistributedTest {@Autowired private RedisService redisService; private class Task implements Runnable {@Override public void run() {if (Integer.valueOf(redisService.get("count")) < 10) {redisService.incr("count"); } } }@PostConstruct public void test() { ExecutorService service = Executors.newFixedThreadPool(16); for (int i = 0;i < 100000;i++) { service.execute(new Task()); } service.shutdown(); } }
我们启动两个进程,两个进程启动完成后,我们再来看一下该键的值。
127.0.0.1:6379> get count
"15"
这个时候我们可以看到,在没有锁的情况下,数量超过了10.
现在用分布式锁来进行测试。
将count键重新设为0
127.0.0.1:6379> set count 0
OK
@Slf4j@Servicepublic class DistributedTest {private SpinDistributedLock lock = new SpinDistributedLock(); @Autowired private RedisService redisService; private class Task implements Runnable {@Override public void run() {try {lock.lock(redisService,"countlock","countlock",3); log.info(Thread.currentThread().getName() + "进入锁"); if (Integer.valueOf(redisService.get("count")) < 10) {redisService.incr("count"); } } finally {lock.unlock(redisService,"countlock","countlock"); log.info(Thread.currentThread().getName() + "释放锁"); } } }@PostConstruct public void test() { ExecutorService service = Executors.newFixedThreadPool(16); for (int i = 0;i < 100000;i++) { service.execute(new Task()); } service.shutdown(); } }
同样启动两个进程或者更多进程,启动完成后,我们来看一下count键的值
127.0.0.1:6379> get count
"10"
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
当前名称:如何实现一个自旋分布式锁
网页链接:http://scyanting.com/article/gdsgpe.html