ReentrantLock$Sync.tryRelease java.lang.IllegalMonitorStateException

早上一来,例行性的看主要环境的运行情况,发现有个环境中有如下异常:

17-02-28 08:13:37.368 ERROR pool-2-thread-65 com.ld.net.spider.SpiderClient.call(SpiderClient.java:75):
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.ld.net.spider.SpiderClient.call(SpiderClient.java:65)
at com.ld.net.remoting.rmq.LDSpiderMultiServiceBeanConfigurer$DealRequest.handleCall(LDSpiderMultiServiceBeanConfigurer.java:677)
at com.ld.net.remoting.rmq.LDSpiderMultiServiceBeanConfigurer$DealRequest.processRequest(LDSpiderMultiServiceBeanConfigurer.java:604)
at com.ld.net.remoting.rmq.LDSpiderMultiServiceBeanConfigurer$2$1.run(LDSpiderMultiServiceBeanConfigurer.java:222)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
at com.ld.net.spider.stat.ServiceStatHelper.writeSlowRequest(ServiceStatHelper.java:80)
at com.ld.net.spider.SpiderRouter.call(SpiderRouter.java:116)
at com.ld.net.spider.SpiderRouter.call(SpiderRouter.java:63)
at com.ld.net.spider.client.RpcServiceProxyImpl.callService(RpcServiceProxyImpl.java:292)
at com.ld.net.spider.client.RpcServiceProxyImpl.invoke(RpcServiceProxyImpl.java:246)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy28.funcl_trd_offer_QuerySecuCancel(Unknown Source)

直接找到对应的源码看了下逻辑,同时网上搜了下,又仔细看了下代码和javadoc,报错的代码如下:

        try {
            if (lock.tryLock() || lock.tryLock(1, TimeUnit.MICROSECONDS)) {
                // 此处为业务逻辑...
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

javadoc中关于lock.unlock的说明如下:

没有注意到释放未持有的锁会导致该异常,更改为如下,问题即可解决。

        try {
            if (lock.tryLock() || lock.tryLock(1, TimeUnit.MICROSECONDS)) {
                // 此处为业务逻辑...
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
posted @ 2017-02-28 08:33  zhjh256  阅读(1234)  评论(0编辑  收藏  举报