gpt4 book ai didi

java - 解锁锁定同步块(synchronized block)的可靠方法

转载 作者:行者123 更新时间:2023-12-01 17:32:58 26 4
gpt4 key购买 nike

系统偶尔会在同步块(synchronized block)上抛出严重错误,并且理论上应该有一个 try-catch 启动线程“解锁”过程,但这不会被触发。因此,我可以得出结论,这在系统内不被视为异常(exception)。有没有合理的方法来处理所述锁?

考虑 board.jsp 中的以下代码:

account = Account.get(accountID);
Object synch=account;

if(synch == null) {
%>No account.<%
return;
}
try {
synchronized(synch) {
....
}
} catch (Exception e) {
....
} finally {
....
}

示例转储

SEVERE: Scheduled tasks not running!
Apr 07, 2020 4:59:36 PM ExecLauncher:execLauncher run
SEVERE: ---------------------
Thread is: "http-bio-8080-exec-18" Id=122 BLOCKED on com.main.Account@5a0b8133 owned by "http-bio-8080-exec-7" Id=38
at org.apache.jsp.realtime.board_jsp._jspService(board_jsp.java:192)
- blocked on com.main.Account@5a0b8133
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

感谢您的浏览:)

最佳答案

您可以尝试一下,您将获得更多控制权来使用可重入锁显式锁定和解锁。

account = Account.get(accountID);
Object synch=account;
Lock lock = new ReentrantLock();


if(synch == null) {
%>No account.<%
return;
}
try {
lock.lock();
/* Your actual code which you were executing in
the synchronized block */
} catch (Exception e) {
....
} finally {
lock.unlock(); // this will work
}

关于java - 解锁锁定同步块(synchronized block)的可靠方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61085803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com