本文主要是介绍attempt to unlock lock, not locked by current thread by node id错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题
Exception in thread "thread-21" java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 59f44274-17cb-40cd-b83b-08ed1ed3ffc7 thread-id: 73at org.redisson.RedissonBaseLock.lambda$unlockAsync$1(RedissonBaseLock.java:320)at org.redisson.misc.RedissonPromise.lambda$onComplete$0(RedissonPromise.java:187)at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:552)at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605)at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)at org.redisson.misc.RedissonPromise.trySuccess(RedissonPromise.java:82)at org.redisson.RedissonBaseLock.lambda$evalWriteAsync$0(RedissonBaseLock.java:232)at org.redisson.misc.RedissonPromise.lambda$onComplete$0(RedissonPromise.java:187)at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:552)at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605)at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)at org.redisson.misc.RedissonPromise.trySuccess(RedissonPromise.java:82)at org.redisson.command.CommandBatchService.lambda$executeAsync$7(CommandBatchService.java:335)at org.redisson.misc.RedissonPromise.lambda$onComplete$0(RedissonPromise.java:187)at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571)at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550)at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605)at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)at org.redisson.misc.RedissonPromise.trySuccess(RedissonPromise.java:82)at org.redisson.command.RedisCommonBatchExecutor.handleResult(RedisCommonBatchExecutor.java:164)at org.redisson.command.RedisExecutor.checkAttemptPromise(RedisExecutor.java:464)at org.redisson.command.RedisExecutor.lambda$execute$3(RedisExecutor.java:172)at org.redisson.misc.RedissonPromise.lambda$onComplete$0(RedissonPromise.java:187)at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:552)at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605)at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)at org.redisson.misc.RedissonPromise.trySuccess(RedissonPromise.java:82)at org.redisson.client.handler.CommandDecoder.decodeCommandBatch(CommandDecoder.java:318)at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:210)at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:137)at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:113)at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:507)at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)at java.lang.Thread.run(Thread.java:748)
翻译
java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 59f44274-17cb-40cd-b83b-08ed1ed3ffc7 thread-id: 70
当前线程没有被锁,但是企图去释放锁。
解决办法
判断是否已锁,且锁是否被当前线程持有
/*** 释放锁* 判断要解锁的key是否已被锁定;判断要解锁的key是否被当前线程持有*/if (lock.isLocked() && lock.isHeldByCurrentThread()) {log.info("线程:" + Thread.currentThread().getName() + "准备释放锁");lock.unlock();}
加锁解锁参考代码
参考代码
@ResourceRedissonClient redissonClient;private int ticket = 100;private final static String LOCK_KEY = "TICKET_KEY";@Overridepublic void hello(String name) {//定义锁RLock lock = redissonClient.getLock(LOCK_KEY);try {//尝试加锁,最大等待时间2秒(如果还没等到锁,则失败),最大持锁时间10秒(防止意外情况无法释放)if (lock.tryLock(2000, 10000, TimeUnit.MILLISECONDS)) {int t = new Random().nextInt(4000);log.info("线程:" + Thread.currentThread().getName() + "获得了锁,占锁时间:" + t + " 毫秒");Thread.sleep(t);log.info("剩余数量:{}", --ticket);} else {log.info("线程:" + Thread.currentThread().getName() + "未获取到锁");}} catch (Exception e) {log.error("程序执行异常:{}", e);} finally {/*** 释放锁* 判断要解锁的key是否已被锁定;判断要解锁的key是否被当前线程持有*/if (lock.isLocked() && lock.isHeldByCurrentThread()) {log.info("线程:" + Thread.currentThread().getName() + "准备释放锁");lock.unlock();}}}
这篇关于attempt to unlock lock, not locked by current thread by node id错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!